4. Constants and variable declarations
The variable declaration of OC uses the way the type variable name = variable value, where the type is a system built-in data type or a custom type, the variable name must start with the English letter and cannot contain special characters
The swift variable declares how to use the var variable name = variable value, where the variable name can use any character you like, even emoji emoji, etc.
OC constant declarations are declared constants, such as: const nsstring *STR = @ "Hello", before the variable is defined with a const;
SWIFT constant declaration uses the Let constant name = constant value, also can use any character you like as a constant name
Swift can automatically infer the data type of a constant and variable, or it can use the ": Data Type" method to specify its specific data type, such as let Age:int = 10
Ps:swift Strict requirements variables must be initialized before they are used, as shown above, the value of the variable must be given, and OC does not enforce
5. Integer
Swift provides 8, 16, 32, 64-bit signed and unsigned integers, such as UInt8, Int64, and so on, each of which has a min and Max callable, such as Uint8.min, Uint8.max, and so on.
6. Floating point number
Swift floating-point numbers can be expressed in decimal and hexadecimal 2.
Decimal: Let d1=12.5 or let D2=0.125e2 suffix E2 represents the front portion of e multiplied by 10 of the 2-time Square
Hex: Let d3=0xc.8p0 0x prefix means hexadecimal, p0 represents the previous entire number multiplied by 2 of 0, and the exponential portion (PX) must be present
7. Number format
Swift allows symbols to be added to the middle of a number to increase readability without affecting the original data, such as let money = 100_0000 or 100000
Swift also allows 0 to be added to the front of the number to increase readability without affecting the original data, such as let-money = 001_000_000 or 1000000
And these are not allowed in OC.
8. String
Use @ "" in OC to represent strings
Swift uses "" to represent strings, stitching strings directly using + to concatenation two strings or string variables, such as Var str1= "abc"; var str2= "def"; var str3=str1+str2;
OC uses the StringFormat method for string formatting, and other non-string values can be inserted into the string for formatting
Swift uses "\ ()" To insert the other non-string into the string, such as: Let Hand=2;var age=20;let str= "I am (age) year old, have \ (hand) Only Hand", can also use the string (age) to convert, and use + to string stitching, such as let str= "I this year" +string (age) + "old, have" +string (hand) + "only Hand"
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 assignment operator returns the value of the variable, such as int a = 0;int b = a = 10; in fact a=10 returns 10 is assigned to B
The swift assignment operator does not return a value
OC modulo arithmetic only supports integer modulo arithmetic
The swift modulo operation supports floating-point calculation, such as 8%2.5=0.5, because 8=2.5*3+0.5
In OC, BOOL has two values YES no actually 0 is false, not 0 represents true
The bool in Swift has two values of false and true and only false and true 0 and non 0 do not represent bool values in any case, such as if (10) The notation is wrong
More than a few operators in Swift than OC
10.1 Closing range operator
a...b [A, b] contains a, b
A.. <b [A, b] contains a, does not contain a
such as for I in 0..<5{
PRINTFLN (i)
}
10.2 overflow operator
Swift provides 5 & start overflow operators for integer calculations
&+ overflow Plus
&-Overflow reduction
&* overflow Multiply
&/overflow except
&% Overflow Seeking mode
such as Let x = Uint8.max//x=255
Let y = x &+1//y=0
such as Let x = Uint8.min//x=0
Let y = y &-1//y=255
The difference between OC and Swift two