Swift Learning notes-common keywords in swift

Source: Internet
Author: User

Common keywords in swift
    • * * Keywords used as a declaration: * *

class deinit ,, init ,,,,, enum extension func import let ,, ,,,, typealias protocol static struct subscript var

    • Keywords to use as statements

break case ,, fallthrough ,,,, , if continue default do else in ,, where ,,,, for return switch while

    • Keywords to use as expressions and types:

as dynamicType ,, Self ,,,, , Type new is super self __COLUMN__ , __FUNCTION__ ,,, __FILE__ __LINE__

    • keywords that are reserved in a specific context :

associativity didset ,, mutating ,,,, , none get infix inout left nonmutating ,, prefix ,,,,, operator override postfix precedence , rightset , ,,, weak unowned unowned(sale) unowned(unsafe) willset

Keyword effect:

class :Used to declare a class
enum :Used to declare an enumeration
init :Modification relative to the class's release method.
deinit :Modification relative to the class's release method.
The construction and release of classes in Swift requires the use of keywords to decorate, and many high-level languages do not require special designations, C + + only requires the same class name as the constructor name, no additional keywords are required.
extension :Extended. Categories similar to OC.
1. The following can be extended in Swift:
2. Adding calculated properties and calculating static properties
3. Defining instance methods and type methods
4. Provide a new constructor
5. Define Subscript
6. Define and use a new nested type
7. Make an existing type conform to an interface
let :Declares a constant. Similar to const
protocol :Protocol. It can also be called an interface. The use of protocols is a good polymorphism in many high-level languages where multiple inheritance is not possible.
static :Declaring a static variable or function
struct :Declaration defines a struct body
subscript :Subscript index Decoration. You can have class, struct, and enum use subscript to access internal values
typealias :Declare an alias for this type. Similar to typedef.
break :Jumps out of the loop. Typically used in a control flow, such as for. Statements such as while switch
case :Switch's selection branch.
continue :Skip this loop and proceed to the back loop.
in :A range or collection operation that is used for traversal.
fallthrough : The swift language feature of the switch statement can be ignored without writing, and will jump out of the loop when the condition is met. Fallthrough's role is to execute the current case, Continue with the following case. Similar to omitting a break in other languages, a case will continue to run until a break or default is encountered.

switch integertodescribe {case 1, 3, 5, 7, 11, 13, 17, Span class= "Hljs-number" >19:description + =  "a prime number, and also"; Fallthrough //does not jump out of the loop, but continues to execute Case5case 5:description + =  "an integer" //perform this step and jump out of the loop  "finished"}        

where : used for conditional judgment, and database query when the WHERE ' ID > 10 ' function. The features of the swift language. Not in OC.

let yetAnotherPoint = (1, -1)  switch yetAnotherPoint {  case let (x, y) where x == y:  println("(\\(x), \\(y)) is on the line x == y") case let (x, y) where x == -y: println("(\\(x), \\(y)) is on the line x == -y") case let (x, y): println("(\\(x), \\(y)) is just some arbitrary point") 

The statement is executed when the condition of the switch satisfies the condition that follows the where.
is & as is generally used to determine the type of some variables. Similar to Iskindclass in OC. As is identical to the cast meaning.
is Example:

for view : AnyObject in self.view.subviews  {      if view is UIButton { let btn = view as UIButton; println(btn) } } 

dynamicType : Gets the dynamic type of the object, which is the actual type of the runtime, not the type that the code specifies or the compiler sees
__COLUMN__: Column number,
* * __FILE__ *: path,
__FUNCTION__function
__LINE__: line number

See Print
associativity: The associative nature of operators
inout: inout A keyword that references a value when it is declared as a function. But the address is referenced at the time of invocation, so when referencing, add &For example:

func test(inout a :Int , inout b :Int){  // 函数内相关操作 }var  num1 = 3var  num2 = 10test(&num1,&num2)

willSet 和 didSet: The role of Willset and Didset is to attach additional operations to the assignment process before and after
It can be seen as capturing state and then doing the operation, when the value is to be assigned and when it is already assigned.
mutating: function: Write in front of Func so that Func can modify the values of the members in the struct and protocol extension. If the values of the members in the extension without mutating,struct and protocol in front of the Func are protected, you cannot modify the
class var: Static variables are supported with the Static keyword for enum and struct in Swift.
However, for class members, you can only return a read-only value in class var. For example:

struct SomeStructure {static var storedtypeproperty =  "Some value." static var computedtypeproperty:int {//return a Int value here }}enum someenumeration {static var storedTypeProperty =  "Some value". static var computedtypeproperty:int {//return a Int value here }}class someclass {class var computedtypeproperty:int {}} 

This actually makes a good distinction between struct and class,
Not like C # Grab a casual use, but compared to OC is actually weakening the boundaries,
If you want to make a static variable that is not read-only in class, you can mate with the struct.
convenience: The convenience is used for convenient initialization, which is equivalent to a constructor overload.
For class, the default or specified initialization method is initialized as the so-called designated.
If the initialization of the overload requires calling designated initialization, initialize it as convenience, and add the convenience keyword before the method.

class Figure{         var name:String! var nikname:String? init(){ name = "John" } convenience init(name:String!,nikname:String!) { self.init() self.name = name self.nikname = nikname } }

precedence: The priority of the operation, the higher the priority of the calculation. The priority of multiplication and division in Swift is 150, and the priority of addition and subtraction is 140, where we define the priority of the dot product as 160, which means that it should be done before the normal multiplication.
unowned, unowned(safe), unowned(unsafe): No host reference.
infix: Indicates that a bitwise operator is to be defined, that is, both front and back input
defer: To wrap a piece of code, this block of code will be called at the end of the current scope. This is often used to clean up the current code, such as closing open files.
Multiple defer can be specified in the same scope
Blocks of code that, at the end of the current scope, are called in reverse order, that is, after the definition is executed, and then the definition is executed first.
guard: When certain conditions are not met, jump out of scope.

func testFunc(input:Int) {         else  {               print("Input must < 10")                return          }          print("Input is \\(input)")} testFunc(1) testFunc(11)

Same as if usage, but the effect is the opposite of If. Guard has one advantage over if: If you don't use Return,break,continue,throw to jump out of the current scope, The compiler will make an error. Therefore, for those who are very strict with the conditions, the Guard is the perfect choice. The guard can also use the optional binding (Optional binding) guard let that is the format

func testMathFunc(input:Int?){    let _ = input else  {          print("Input cannot be nil")          return }} testMathFunc(nil)

Ps:set, get, new, self, super, none, if, for, return are not mentioned in the article. Not mentioned are the most basic or I do not know or infrequently used keywords, behind the contact with the new record.



Huaqianyuexia
Links: https://www.jianshu.com/p/1ae8c364954a
Source: Pinterest
Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.

Swift Learning notes-common keywords in swift

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.