What is the difference between OC and Swift? What is the difference between OCSwift?

Source: Internet
Author: User

What is the difference between OC and Swift? What is the difference between OCSwift?

4. constants and variable declarations

The oc variable Declaration uses the type variable name = variable value method. The type is the built-in data type or custom type. The variable name must start with an English letter and cannot contain special characters.

Swift variable Declaration uses var variable name = variable value, where variable name can use any character you like, or even emoji

The oc constant Declaration uses const to declare a constant before the variable definition, for example, const NSString * str = @ "Hello ";

Swift constant Declaration uses let constant name = constant value. You can also use any character you like as the constant name.


Swift can automatically deduce the Data Types of constants and variables. Of course, you can also use the ": Data Type" method to specify the specific data type, such as let age: Int = 10

PS: swift strictly requires that variables must be initialized before use. As shown above, variable values must be specified, while oc is not mandatory.

5. Integer

Swift provides 8, 16, 32, 64-Bit Signed and unsigned integers, such as UInt8, Int64, etc., each type has a min and max adjustable, such as UInt8.min and UInt8.max.

6. Floating Point Number

Swift floating point numbers can be expressed in decimal or hexadecimal notation.

Decimal: let d1 = 12.5 or let d2 = 0.125e2 suffix e2 represents the first part of e multiplied by the 2nd power of 10

Hexadecimal: the prefix of let d3 = 0xC. 8p0 0x indicates that the latter is a hexadecimal system. p0 indicates that the first digit is multiplied by the 0th power of 2 and the exponent part (px) must exist.

7. digit format

In swift, you can add the symbol _ in the middle of the number to increase readability without affecting the original data. For example, let money = 100_0000 or 100000

Swift also allows adding 0 to the front of the number to increase readability and does not affect the original data. For example, let money = 000000000_000 still indicates 1000000.

These are not allowed in oc.

8. String

In oc, @ "" is used to represent strings.

Swift uses "" To represent strings. concatenate two strings or string variables using + directly, for example, var str1 = "abc"; var str2 = "def "; var str3 = str1 + str2;

Oc uses the stringFormat method to format strings. Other non-string values can be inserted into strings for formatting.

Swift uses "\ ()" to insert other non-strings into the string, such as: let hand = 2; var age = 20; let str = "I am \ (age) years old, with \ (hand) hand ", you can also use String (age) for conversion, and use + for String concatenation, such as let str =" "+ String (age) + "years old, with" + String (hand) + "hand only"

9. Data Type Conversion

Swift type conversion uses "data type (original data)" for type conversion, such as let money1 = 100; let money2 = 50.5; let totalMoney = Double (money1) + money2;

10. Operators

The oc value assignment operator returns the variable value, for example, int a = 0; int B = a = 10; in fact, a = 10 returns 10 to B.

The swift assignment operator does not return values.


Oc modulo operation only supports integer modulo operation

Swift modulo operations support floating-point modulo operations, such as 8% 2.5 = 0.5, because 8 = 2.5*3 + 0.5


Bool in oc has two values: yes no. Actually, 0 is false, and non-0 represents true.

In swift, bool has two values: false true and only false and true. 0 and non-0 do not represent bool values in any situation. For example, if (10) is incorrect.


Swift has more operators than oc.

10.1 close range Operator

A... B [a, B] contains a, B

A .. <B [a, B) contains a, not B

For example, for I in 0 .. <5 {

Printfln (I)


10.2 overflow Operator

Swift provides five overflow operators starting with & for integer computing.

& + Overflow

&-Overflow reduction

& * Overflow Multiplication

&/Overflow Division

& % Overflow modulo

For example, let x = UInt8.max // x = 255

Let y = x & + 1 // y = 0

For example, let x = UInt8.min // x = 0

Let y = y &-1/y = 255

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.