Swift basic syntax

Source: Internet
Author: User
Tags unpack

Swift basic syntax

1. Basics
1.1) swift still uses // and/**/for comments, and/**/allows multi-line comments.
1.2) swift uses print and println for printing. Its parameter passing is generic and almost all types are printable.
1.3) whether or not the plus points are correct after the statement, but the preferred style of swift is not at the end. If there are multiple statements, they must be separated by semicolons.
1.4) A hyphen (_) In a number will be ignored, so that you can easily recognize a number with a large value.
1.5) swift does not allow addition, subtraction, multiplication, and division between different types. type conversion or operator overloading must be performed first.
1.6) typealias can specify another name for a type to clearly express the meaning of the type. typealias newType = OldTypeName
1.7) assert assertions indicate that an exception is thrown. The first parameter of assert indicates that an exception is passed but not thrown. The second parameter indicates a prompt. The second parameter may not
1.8) The = value assignment operator in swift has no return value, so it will not make the if a = 3 {} error. Add spaces before and after the operator to avoid xcode being unrecognizable.
2. Variables
2.1) let defines constants and var defines variables. let defines the initial values during declaration, and ordinary var statements must also assign the initial values.
2.2) let declared variables cannot be changed, while var variables can be changed, but you cannot declare a declared constant or variable.
2.3) Multiple constants or variables can be declared in a row at the same time, separated by commas. If each constant corresponds to a var or let, you only need to use semicolons to separate it.
2.4) each constant and variable must have a fixed type. If no type is specified, swift will deduce it based on the subsequent value assignment type.
2.5) the name of swift cannot start with a reserved word or arrow or number, and can even be named by a puppy or kitten.
2.6) If you want to use keywords for naming, it is not allowed, but you can add ''before and after naming, such as 'let'. It is also possible to add'' for non-Keyword naming.
2.7) You can declare tuples in swift. The type is similar to (Int, String). You can declare them using var or let. Both of them are variables or constants.
2.8) There is no big difference between the two variables and the constant declaration in the way of tuples and directly separate them. You can take out the variables here for separate use or use them as tuples. You can also use them together.
2.9) You can directly access the corresponding values in the tuples by using the subscript index, such as aaa.0 aaa.1.
2.10) You can name the elements in the tuples, for example (code: 404, message: "Not Found"), and then you can access them using aaa. code and aaa. message.
3. Type
3.1) int-type UInt8 and Int32 can use min and max to obtain the maximum and minimum values.
3.2) the Double is a 64-bit floating point number, and the Float is a 32-bit floating point number. It is automatically inferred that the decimal number is a Double type, unless it is an excessively typed
3.3) You can assign an integer to a constant or variable specified as a floating point type but not an integer variable. It is automatically converted to a floating point number.
3.4) it indicates that a variable or constant of the specified type cannot be assigned a different type value, and it cannot be converted automatically, except for an integer value assigned to the floating point type.
3.5) Add? Indicates the optional type, which means that it may be null or not, and you can use it by optional binding or determining whether it is null.
3.6) if it is an optional class type, you can use .? To access its attributes and methods, it is through whether the former will be followed by the corresponding method, if the response can be executed, if not, it will return nil
(3.7 )? It is actually a syntactic sugar, such as String? The type is equivalent to the Optional <String> type, which is easy to write. It is essentially different from the String type.
3.8) If you do not want to determine whether it is null or use optional binding or use .? To access it, you can use the most direct and simple! Force unpack to use, but ensure that it is not empty
3.9) An error is returned if an optional value is not set and the packet is forcibly unwrapped. An optional type is implicitly assigned as nil. You can also assign a value to nil in use.
3.10) the reason why you need an optional type is that swift is an absolutely secure language of the type. It requires you to have a value when using the variable, but this type of choice is too troublesome, so it is generated! Type ,! Type should be optional in essence
3.11) declared! Optional type, or Initialization is not required. It is equivalent to adding the optional type each time you use it! Force unpack, you do not need to add it yourself! You must make sure it is not empty. Otherwise, an error will occur.
4. String
4.1)... indicates the closed interval/... <indicates the open interval, = indicates that the value is equal/=== indicates that the reference is the same, + you can directly add a string or Array
4.2) the string type in swift is a value type. It copies the value when being assigned to a constant variable or passed in a function. It is a new copy, swift only copies the data if necessary.
4.3) you can use for in to traverse strings, use the count global function to calculate the number of characters, use the isEmpty attribute to determine whether the string is null, and use hasPrefix to determine the prefix (suffix, etc)
4.4) You can use startIndex and endIndex to obtain the start and end subscript, access a single character in the form of an array and a lower mark, and generate a string using \ ().
5. Array
5.1) arrays in swift can be declared using [String] and Array <String>. They share the same meaning, and the value assignment can be directly expressed using brackets, the value must be of the same type.
5.2) array has attributes such as count/isEmpty and append/insert/remove. It can directly add an array, which can be determined by subscript and subscript range, the range is an array.
5.3) for item in shoppingList/for (index, value) in enumerate (shoppingList) Two traversal methods, the latter will know the subscript
5.4) You can use [Double] (count: 3, repeatedValue: 3.3)/Array (count: 3, repeatedValue: 2.5) to initialize an Array.
5.5) The array is also a value transfer. If array a is assigned to array B, and array a and array B are two copies, the modification will not affect each other.
6. Dictionary
6.1) the Dictionary is declared through [String: String]/Dictionary <String, String>, and the form is also in the form of [a: B, c: d ].
6.2) You can add a key-Value Pair directly using a [B] = c. You can use updateValue and removeValueForKey to add or delete a key-value pair.
6.3) you can use for (airportCode, airportName) in airports/for airportCode in airports. keys to Traverse Key-value pairs or keys or values.
6.4) You can directly assign a value [:] to clear the dictionary. The key value can be Int. Similar to string and array, dictionary also copies the value.
7. Set
7.1) use Set <Int> to declare the Set, which is exactly the same as the array, and assign values to it. You can also assign values to [] to clear the Set, which must be of the same type. The insert/contains method and isEmpty attribute are also available.
7.2) Set also has a for in traversal, a method to take the intersection of two sets for arrangement, and a method to determine the subset superset.
8. For Loop
8.1) the index for index in 1... 5 is only in the scope of the current loop. If there is an index outside the for loop, any modification will not affect it.
8.2) for _ in 1... 10. If this index does not need to be recycled, it can be replaced by underscore _. in swift, as long as it does not need that value, it can be replaced _.
8.3) for var index = 0; index <3; ++ index this for loop is also possible
9. Switch statement
9.1) the switch in swift must enumerate all possibilities; otherwise, the default value must be added. When the switch is exhaustive, many possible values can be placed after a case, which can be separated by commas.
9.2... and .. <indicates a range. For tuples, one can use the _ wildcard, the other can match, or both can match, or both are intervals (only one element can be bound with values)
9.3) You can use var x/let x or let (x, y) to bind the value during configuration. The value declared by var can be modified.
9.4) when binding a value as a let, you can add the where clause to determine the condition. the switch does not need to use break to prevent penetration, but it still uses the next case to determine the condition to match, and can still directly jump out of the switch using break.
9.5) You can use break/continue to jump out of a tag in a switch.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.