OC and swift summary, ocswift Summary
1. What is the OC language?
The OC language is an object-oriented language. It extends the ansi c language and adds the SmallTalk message transmission mechanism to ansi c. it is the main programming language for Apple OS and iOS and related APIs, Cocoa and Cocoa Touch.
Features of the 2 OC language:
- C language syntax is a superset of ANSI-C language, is an efficient programming language.
- It is an extension of C language object-oriented and fully supports the object-oriented features.
- The simple and easy-to-read programming style makes programming and reading clearer.
- Good compatibility. You can directly import the header files of C and C ++ in the project, or directly use OC and C ++ programming in the file.
3. the iOS ecosystem refers not only to the richness of software, but also to the diversity of its devices.
4 Core OS
It is a Darwin written by FreeBSD and Mach, and is a UNIX core that complies with POSIX standards. this layer includes all the basic functions of iOS (all these functions are provided through the C language API ). in addition, this layer has UNIX characteristics. Therefore, Core OS APIs are used when UNIX functions are transplanted to iOS. at the same time, this layer also provides interfaces between the hardware and the system framework, but for security considerations, only a limited framework can be used.
5 Core services
It provides more functions based on Core OS, including Foundation. framework and Core Foundation. framework. it is called Foundation because it provides basic functions such as processing strings, arrangement, and time .). among them, Foundation belongs to the oc api, while Core Foundation belongs to the c api. in addition, Core Sercies also provides Security (processing authentication, password management, Security management), Core Location (positioning) SQLite (lightweight database), Address Book (processing phone Book) and other functions.
6 Media
It provides multimedia functions such as images, music, and videos. the images include 2D (Quartz 2D) and 3D (Open GLES ). the modules corresponding to music are Core Audio and Open AL. media Player is used for video support. at last, Core Animation is supported.
7 Cocoa Touch
It is the UIKit of the core of OC APIs. framework, all the components on the application are implemented by it. in addition, it is responsible for multi-point touch. text output, image web page display, camera/File Access, and accelerated sensing.
8 swift
It inherits the object-oriented advantages of OC and has a simple syntax structure. It has the following features.
- Unification of closures
- Ancestor and multiple return values
- Generic
- Fast and concise iteration range/Set
- Supported methods and extended protocols
- Functional programming mode
- You can view the effect without compiling.
- Efficient and powerful
- Security
9 differences between OC and Swift
- No extra points are required at the end of a Swift sentence, unless several lines of code are written in one line.
- Swift does not have the main function. The function is executed from top to bottom.
- Swift does not have. h and. m Files, only. Swift files.
- Swift has no address Concept
- The Swift data type is automatically determined (var is used for variables and let is used for constants)
- Forced type conversion: Strong conversion of OC to a (int) a, Swift to a int ()
- You can use. min and. max to obtain the maximum and minimum values of integer data types.
- Definition type: OC: Typedef int Myint Swift: Typealiaes Myint = int
- Swift supports Decimals in the remainder operation.
- In Swift, the BOOL type is true if it is not 0 in OC, but false if it is true or false.
- Swift's value assignment operation does not return values.
- Swift can assign multiple to multiple values: let (x, y) = (1, 2)
- The Swift loop statement must be added with {}. Even if there is only one line of code
- The switch statement of Swift can be followed by any data type without the need for break.
Note: If you do not want the break effect, that is, if you want to execute all the subsequent tasks, write the keyword fallThrough. However, you cannot define constant variables after fallThrough.
10 unique features of swift
- Operation:... B Indicates [a, B] a .. <B indicates (a, B)
- Exclusive overflow OPERATOR: When you assign a value greater than the maximum value range, an error is returned.
- Unique Ancestor Type of Swift
Var a = (x: 15, y: 20.0)
A is the ancestor name, which contains two elements x and y.
Assign values: a. x = 10 or a.0 = 10 (subscript exists by default)
Change type: var a = (int, string) = (10, 21.0)
Print: printIn (a) = (10, 20.0)
Omit unnecessary element var a = (_, 20.0) with an underscore)
- When the Switch statement is similar to the Ancestor Type, you can also use the syntax similar to the SQL statement to add filter conditions.
- External Parameter Name of the Function
Original form: func Sum (num1: int, num2: int) à int {} call: Sum (10, 20)
Now: func Sum (numOne num1: int, numTwo num2: int) à int {}
Call: Sum (numOne: 10, numTwo: 20)
- Default function parameter value
Func addPerson (name: string, age: Int = 18) à string {}
Call: addPerson ("tom ")
Note: addPerson ("Tom", age: 19)