Swift, Apple's new development language, released in 2014 at the WWDC (Apple Developer Conference), works with objective-c* on Mac OS and iOS platforms to build apps based on Apple platforms.
It has been updated to Swift2.0 and is about to release 3.0. I heard there was a lot of change. Now let's look at the small similarities and differences between Swift and OC basics (but if it's different from OC, it feels very different.)
1. Different output logs
2. Constant variable definitions, Swift type is more accurate than OC, Swift is a type-safe language, Swift does not allow implicit conversions, but can display conversions
3. Operator: +,-, *,/,%, + +,--, OC and Swiftch In addition to the modulo operator, the OC modulo must be an integer, Swift modulo can be a decimal, Swift cannot be continuously assigned, but OC can
4. Branch
@1
1), if the parentheses after the can be omitted
2), if after can only receive bool value
3), if the curly braces cannot be omitted
@2
Swift and OC cycles are similar, but swift,for parentheses can be omitted
@3
1) While loop: the parentheses after the while can be omitted, only the bool value as the conditional statement
2) Do-while,swift in OC is used to catch anomalies, Swift uses repeat-while
There is a tuple in 4.swift, OC Wood has
5.swift Nil and OC somewhat different, OC only objects can be used Nil,swift base type (shaping, floating point) No value is also nil, when initialized, Swift can have no initial value, resulting in an optional value optional.
6.oc/swift characters are "/" ", String nsstring/string @" "/" ", OC ends with/0 string, Swift is not
1.OC output Log with NSLog
Swift prints out logs with print
Swift statement end does not need to add;
2.OC defines a constant const int a = 10;
Swift defines a constant let A = 10
OC defines the variable int a = 10;
Swift defines variable var a:int = 10
Determine the type, and then assign the value
var a:int
A = 20
Print (a)
Let A:float = 10.3//indicates 32-bit floating point type
Let b:double = 10.2//indicates 64-bit floating point type
Swift type is more accurate than OC
Let Intvalue1:int
Let Intvalue2:int16
Let Intvalue3:int32
Let Intvalue4:int64
Let Uintvalue:uint
Swift is a type-safe language, and if a direct assignment error is direct, OC will not
Let Uintvalue:uint =-10
Swift does not allow implicit conversions, but can display conversions
var a:int = 10
var b:float = Float (a)
Range operators:
Enclosing range operators:
Includes closure interval all values a...b
Semi-enclosing range operators:
Contains header does not contain tail a. <b
Scenario: Iterating through an array
For I in Arr {
}
For I in 1...5 {
Print (i)
}
For I in 1..<5 {
Print (i)
}
Bool:
C and OC do not have a true bool value
C 0 False not 0 is true
OC BOOL type typedef signed CHAR BOOL
Swift REAL BOOL
It's ture false.
If True {
Print ("Yes")
} else {
Print ("No")
}
3.
do {
index--;
}while (Index > 0)
Do in Swift is used to catch exceptions.
Repeat {
index--
Print (index)
}while (Index > 2)
4. Tuples: Enclosing multiple values of the same or different types in a single parenthesis is a tuple. Tuples and structs are similar, in fact tuples are composite types. Parentheses can be written in any type, if you do not define the type, you can automatically determine the type according to the data
The type is omitted
Let person = ("YCG", 17,180.00)
No omitted type
Let Person1: (string,int,double) = ("YCG", 17,180.00)
Get an item in a tuple
Print (person.0)
Print (PERSON.1)
Print (person.2)
The above tuple definitions have great flaws and are particularly poor in readability.
Define tuples in other ways
Omit type
Let Person2 = (name: "YCG", age:17,height:80.00)
No omitted type
Let Person3: (name:string,age:int,height:double) = (name: "YCG", age:17,height:80.00)
Get the corresponding value
Print (Person2.name)
Print (Person3.height)
Other notation for tuples
Let (name,age,height) = (name: "YCG", age:17,height:80.00)
Print (name)
Print (age)
Print (height)
5.
Defining an optional value is easy, just add one after the type. On the line. For example: Var value:int?
The difference between a optional value and a non-optional value is that the optional value is not initialized, although it is nil, but the normal variable does not even have nil.
var value:int?
Print (value)//nil
var value:int
Print (value)//Direct error
Extract the value of an optional type (forced parsing)
The optional value cannot be used directly, because the optional value has two states, one value and no value, so we need to tell the compiler if there is a value
Forces the value of an optional type to be resolved by simply adding it after the variable! We can.
var value:float?
var value2:float = value!
Print (value2)
Note: A run-time error is reported if value has no values.
When I don't know if optional has a value, how do I do it?
If the value is not nil, then the subsequent code is executed.
var value:int? = 10
If let Tmpvalue = value {//define an intermediate constant first, value does not have a value immediately and does not crash
Print (Tmpvalue)
}
Optional:??, he can make a quick judgment on nil,?? The values that follow are those that participate in the calculation
var num:int?
Let Result:int = num?? 0
Print (Result)
6.
Common methods of strings
1. Initialize empty string
Let emptystring = ""
Let emptyString2 = String ()
2. Traversing a string
Let string2 = "You are our good friend!" "
For a in string2.characters {
Print (a)
}
3. Get the length of the string
Let count = String2.characters.count
Print (count)
4. Determine if the string is empty
Let emptyStr = "1"
If Emptystr.isempty {
Print ("Yes")
}
5, the concatenation of strings
Let str1 = "Hello"
Let str2 = "world! "
Let result = str1 + str2
var str3 = "haha"
STR3 = Str3 + str2
Print (STR3)
6. Formatting strings
Let Intvalue = 10
Let STR4 = "Love"
Let STR5 = "\ (intvalue), because \ (STR4), will not be sad"
Print (STR5)
Take a decimal and then two digits
Let pi = 3.1415
Let STR6 = String (format: "%.2f", Pi)
Print (STR6)
7. String comparison
Same as the C language strcmp
Let STR7 = "a"
Let str8 = "AB"
If STR7 > Str8 {
Print (">")
} else if str7 < str8 {
Print ("<")
}
8, whether there is a prefix
Let STR9 = "www.baidu.com"
If Str9.hasprefix ("www") {
Print ("Yes")
} else {
Print ("No")
}
If Str9.hassuffix ("cm") {
Print ("Yes")
} else {
Print ("No")
}
9. Case
Let Str10 = str9.uppercasestring
Let Str11 = str9.lowercasestring
Print (STR10)
10. String to basic data type
Let Str12 = "5.2"
Let num = Float (str12)
Print (num)
11. Intercepting strings
Let Str13 = "You are our good friend!" "
Str13.startindex starting from 0 index is not an int
Let Fromstr = Str13.startIndex.advancedBy (2)
Let Tostr = Str13.endIndex.advancedBy (-2)
Let Str14 = Str13.substringfromindex (TOSTR)
Let Range:range <String.Index> = Range (Start:fromstr, END:TOSTR)
Let Str15 = Str13.substringwithrange (range)
Print (STR15)
Let ToStr1 = (Str13 as NSString). Substringtoindex (2);
Print (TOSTR1)
Let Range1 = Nsrange (Location:2, Length:6)
Let TOSTR2 = (Str13 as NSString). Substringwithrange (Range1)
Print (TOSTR2)
A brief talk on the similarities and differences between OC and Swift