1.1. About Swift
· Swift is a new programming language for IOS and OS X apps, built on the best C and Objective-c languages, without the C language compatibility restrictions. Swift uses a secure programming model that adds modern functionality to make programming easier, more flexible, and more fun. Swift is supported by a mature and pampered Cocoa and Cocoa Touch Framework, an opportunity to reimagine software development.
· Supports playground, which allows programs to be previewed in real time without the need to create and run apps frequently
· Simple, safe, easy, flexible, efficient
1.2.swift
In general, the first program in a programming language tutorial should print "Hello, World" on the screen. In Swift, one line of code can be implemented:
1. println ("Hello,world")
Note: To get the best experience, use the Code preview feature in Xcode. The Code preview feature lets you edit the code and see the results of the run in real time.
· Single-line Comment
Single-line Comment
· Multi-line comments
/*
Single-line Comment
/* Multiline comments, note that multiple lines of comments can be nested, this is not the same as other languages, very useful one function */
*/
· Semicolon
· There is no need to add a semicolon after a single statement, plus a line
· Multiple statements are placed on one line, you need to add semicolons
1.3. Variables and constants
· Variable
· Declare a variable with var (the amount that the value can be modified as needed is called a variable)
· Constant
· Declaring a variable with let (the amount that the value cannot be modified by two is called a constant)
· Type deduction
· The compiler automatically infers the type of a particular expression at compile time by providing the initialization value
· Attention
· Variables are assigned multiple times to ensure that the type of the value is the same
· Show claim type, syntax: "Variable: Type"
· Swift does not automatically assign an initialization value to a variable, which means that the variable does not have a default value, so it is required to be initialized before the variable is used.
· Swift does not support hermit type conversions and requires display type
· Use the Typealias keyword to define type aliases, similar to typedef
· string formatting, using \ (item)
1.4. Naming rules
· Almost any character, including Unicode, can be used in Swift for constants and variable names. But it cannot contain numbers, arrows, invalid Unicode, dashes-, tabs,
and cannot start with a number
1.5. Integral form of expression
· Binary number, prefixed with 0b
· Octal number, prefixed with 0o
· Hexadecimal number, prefixed with 0x
2.1. Arrays and dictionaries
· Declaring an array using the [] operator
· Declaring a dictionary using the [key:value] operator
2.1.1. Use of Arrays
2.1.2. Adding, modifying, and deleting an element's value usage
2.1.3. iterating the array
2.1.4. methods for declaring arrays
2.1.5. the Declaration and output of a dictionary
2.1.6. How to Add, modify, and delete dictionaries
2.1.7. Number of output dictionaries count
2.1.8. How to traverse a dictionary
3.1. Yuan zu
Tuples (tuples) combine multiple values into a single composite value. Values within a tuple can be of any type and are not required to be of the same type.
Example: 1. Let Http404error = (404, "not Found")
2.//The type of Http404error is (Int, String), the value is (404, "not Found")
You can combine any sequence of types into a single tuple that can contain all types. As long as you want, you can create a type (int, int, int) or (String, Bool)
or any other group of tuples you want.
You can decompose the contents of a tuple (decompose) into separate constants and variables, and then you can use them normally:
1. Let (StatusCode, statusmessage) = Http404error
2. println ("The Status code is \ (StatusCode)")
3.//Output "The status code is 404"
4. println ("The status message is \ (StatusMessage)")
5.//Output "The status message is not Found"
If you only need a subset of the tuple values, you can mark the part you want to ignore with an underscore (_) when decomposing:
1. Let (Justthestatuscode, _) = Http404error
2. println ("The Status code is \ (Justthestatuscode)")
3.//Output "The status code is 404"
In addition, you can use the subscript to access a single element in a tuple, with the subscript starting from zero:
1. println ("The Status code is \ (http404error.0)") 2. Output "The status code is 404"
3. println ("The status message is \ (Http404error.1)") 4. Output "The status message is not Found" You can name a single element when you define a tuple:
Let Http200status = (statuscode:200, description:)
After naming the elements in a tuple, you can get the values of these elements by name:
1. println ("The Status code is \ (Http200status.statuscode)")
2.//Output "The status code is 200"
3. println ("The status message is \ (http200status.description)") 4. Output "The status message is OK"
Note: Tuples are useful for temporary organization of values, but are not suitable for creating complex data structures. If your data structure is not being used temporarily,
Use a class or struct rather than a tuple. Please refer to class and struct body.
4.1. Optional type
4.1.1.nil explanation
4.1.2. Summary? The usage scenario
· Declaring optional variables
· Used in the optional value operation to determine whether to respond to subsequent operations
5.1. Unpacking
5.1.1 Optional types of implicit unpacking
An optional type example of an implicit unpacking:
6.1.Swift Basic Operators
6.1.1. Assignment Operators
6.1.2. Mathematical operators
6.1.3. comparison Operators
6.1.4. Three mesh operator
6.1.5. Scope Operators
6.1.6. logical Operators
7.1. Characters and strings
7.1.1. Initializing an empty string
7.1.2. Variable-length strings
7.1.3. Character Count
7.1.4. Combining strings with characters
7.1.5. String formatting
7.1.6. String comparisons
7.1.7. String case
7.1.8. String encoding
8.1. Process Control
9.1. Conditional statements
9.1.2. Interval range Matching (range Matching)
10.1. Functions and closures
Main content: • Function definition and invocation • Function parameter and return value • Function parameter name • Function type
• function nesting • Closure Expressions • Trailing closures • Value capture
10. 1.2. function
The notation of C-language function pointers
Block notation for OC language
• Note: The Swift language calls the C function, OC function to introduce the following file
10.1.3. Closures
Summary of closures
Ha ha?? , which is a detailed collation of Swift's language base, and hopes to help swift language learners.
Thank you for your support. Thank you
A basic explanation of swift language