The main difference between Swift and OC is the grammatical differences, the others are probably the same .
Difference 1: use let to define constants in Swift, VAR defines variables. Once a constant is assigned, it cannot be modified, and the variable can be changed after it is assigned.
Swift can tear down the exact type of the variable according to the code on the right.
are there more constants or variables used in real -world development?
The use of constants, more secure, can not be modified, when the need to modify the object is modified to the Var modifier difference.
2: Swift has strict data requirements and does not make implicit conversions;
It is not possible to directly calculate between different types need to manually convert the type, call the constructor pass parameter for type conversion
Difference 3: One of the major features of Swift is the optional option, which defines whether the variable can have a value of a specified type, or nil, if an optional option is specified;
- Disclaimer: Var a:int?
- ‘?‘ Represents an optional type or optional unpacking,
Optional unpacking is used when invoking the properties or methods of an object of an optional type
- '! ' means forced unpacking
- Gets the non-null value of an optional type
- Forced unpacking is risky and needs to be considered safe
'?? ' Merge Empty Options
- Need to use () to increase priority
- If there is a value to use directly, if not used?? Default values that are set later
- Judge a string or basic data
Difference 4: The constructor in Swift can be overloaded, that is, init parentheses write a bunch of parameters, oc in lieu of the way initwithxxx ....
Closed Package
- String
- String is struct, more lightweight, more efficient, supports traversal, can and nsstring seamlessly convert as
- NSString Integrated from NSObject is an object
- Merging of strings
- Use +
- Instantiating a string using string () arguments
- \ () Escape string
- Format the string (format:
- Interception of strings
- Collection
- Let declares an immutable set, Var declares a mutable set
- Array
- [XXX] Represents an array type, XXX is a class
- [XXX] () to instantiate an empty array
- [element 1, Element 2,....]
- Dictionary
- [Key1:value1, Key2:value2,...]
- [String:any] Most commonly used dictionary types
- The basic format of closures
- No parameter no return value (), ()
- There are no return values for parameters (int, int), and ()
- int (int, int) with parameters with return value
-
- Trailing closures
- When the last parameter of the function is a closure, the function's parameter ' () ' can be closed prematurely, if the function is only a closure parameter, ' () ' can omit
The main differences between OC and Swift