The-swift-programming-language Study Notes

Source: Internet
Author: User

Constants and variables

Constants are constants defined in a class that are not modifiable, and can be assigned in constructors. Let decoration
Variables can be modified. var modifier
Traversal of characters in a string
For code in string {}
For codeunit in String.unicodescalars {}

Control statements

Break jumps out of the loop body
Continue terminate current, go to the next loop
The label configuration is used for break and continue, the loop body is marked with a label, and the loop body of the label jumps out of the tag, continue label is the loop that enters the next tag
Used in the Fallthrogh:switch statement, which is used after the case node method to run through the next case
The switch statement must contain the default node, and the case node does not need to use break

Range use

Number1...number2 = = Number1<=var<=number2
Number1. <number2 = = Number1<=var<number2

Meta-group

var tuples = (Int,string,obj,...)

Dictionary

Dictionary<kyet,valuet>
for (Key,value) in Dictionary {}
For key in Dictionary.keys
For value in Dictionary.values
When the dictionary is copied, it depends on whether the dictionary value is a type or a reference type

Function

Func Function (params), params {method body}
The parameters of the function are let constants, the variables need to be defined in the parameters Yes, VAR is defined
Parameter definition, you can assign a value of the default func Test (paramens:string = "Default value") {}
Variable parameter func sum (numbers:double ...) similar to passing a array<t> parameter
Parameter Passing reference inout keyword
Returns a multivalued value (returns a value of a tuple type)
The function itself can be passed as a parameter or as a range value
Internal name and external name of the parameter

function type nesting function

functions defined in other function bodies

Closed Package

Similar to Lambdas expressions
A global function is a name, but does not catch any worthy closures
A nested function is a moniker that has a name and can capture its enclosing function in a domain that is worthy of closure
A closure expression is a closed packet that is written in lightweight syntax that captures the names of variables or constants in its context.
An expression:
{(parameters)-ReturnType in
Statements
}
var numbers =[1,3,30,16,25,73,97]
Func Backwards (s1:string, s2:string), {return s1>s2}
Passed as a function parameter
Sort (numbers,backwars)
Closure expression pass-through
Sort (numbers,{
(S1:string, s2:string), BOOL in
Return S1>S2
})
Writing a line for short closures
Sort (numbers,{(s1:string, s2:string), bool in return S1>S2})
Closure can automatically infer parameter types
Sort (numbers,{s1, S2 in return s1>s2})
Single-line expression can omit the return keyword
Sort (numbers,{s1,s2 in S1>s2})
Abbreviation of parameter name
Sort (numbers,$0>$1)
operator functions
Sort (numbers,>)
Trailing closures: passing closures as the last parameter to a function, you can use trailing closures
Sort (numbers) {
(S1:string, s2:string), BOOL in
Return S1>S2
})
Sort (Numbers) {$0>$1}

Structures and Classes

There are only similar reference types in Swift, others are value types

    • Common:

Defining properties
Defining methods
Define Subscript
Defining constructors
extension method (external)
Compliance Agreement (Protocol)

    • Different:

Class allows inheritance
Type conversions allow you to check and interpret the type of a type instance at run time
Destructors allow an instance of a class to release any resources it allocates
Reference count allows multiple references to a class

Property

Property default values can be set by closures and functions

Store Properties

A constant (let definition) or variable (var definition) stored in an instance of a particular class or struct

Constants and Storage properties

Define a constant structure property, and all the properties of the struct become constants, not the same as the class of the referenced type, after assigning a class instance to a constant, you can still modify the instance's variable properties

Deferred storage properties

Using @lazy to represent a deferred storage attribute, the deferred storage attribute must be declared with Var, and when the value of the property depends on an external factor that cannot be known before the instance is constructed, or when the value of the attribute requires a complex and large number of calculations, it can be calculated only when needed

Calculated properties

Instead of storing the value directly, the computed property provides a getter to get the value, and a setter to indirectly set the value of the other property or variable. The default use of newvalue in setter means the value received

Read-only computed properties

is just a getter. Computed properties

Property Listener

Willset is called before a new value is set. Use NewValue to represent new values by default
Didset is called immediately after the new value is set. Use OldValue to indicate old values by default

Global variables and local variables

The schema that the computed attribute and property listener belongs to can also be used for global variables and local variables, which are variables defined outside of a function, method, closure, or any type, and local variables are variables defined inside a function, method, or closure
Global constants or variables are deferred calculations, similar to deferred computed properties, where global variables or constants do not need to be tagged with the @lazy attribute; local scope constants or variables do not delay calculations.

Type property

Type properties are used to define data shared by all instances of a particular type
For value classes (enumerations and structs) you can define stored and computed type properties, and you can only define computed type properties for class
Declaring a type property with static in enumerations and institutions
Declaring a Type property in class with Class

Method

A property of a value type cannot be modified in his strength method, and if you decide that you need to modify a property of a specific value type, you can choose the mutation method (the method declared with mutating), and then the method can modify its properties from within the method.

Type method

Same as the Type property

Subordinate script subscript

Grammar:
Subscript (Parameter:parametertype), ReturnType {
get{
}
Set (NewValue) {
}
}

Constructors

Defining variables or constant properties in classes and structs must be assigned in the constructor, or assigned when defined
Constant properties can be modified in the constructor

Optional attribute Types

With type? Declaration, the default value is nil

Auto reference count (ARC)

Swift uses ARC to track the memory used by the management app. Most of the time, it means that in swift, memory management is still working and does not need to consider the memory management itself, and when the instance is no longer being used, ARC will automatically release the memory of the class instance.
Resolving strong reference rings for class instances
Weak references (using weak declarations if referenced)
No primary reference (use unowned to declare no primary reference)
Non-primary reference and optional properties for implicit expansion (optional attribute type! for implicitly expanded)
A strong ring reference produced by a closure: assigns a closure to a property of the class instance, and the closure uses an instance. This closure may access a property of the instance, such as Self.someproperty, or call a method, for example: Self.somemethod ().
Resolves a strong ring reference generated by a closure closure, defining a possessive list (declared with [Unowed/weak Self])
@lazy var property: (parametertype), returntype = {
[unowned Self] (Parameter:parametertype), returntype in
}
Closures are defined as unowned when instances of closures and possessions are always referenced to each other and destroyed at the same time. Conversely, when possession references may sometimes say nil, the possession within the closure is defined as weak. A weak reference is always an optional type.
A weak reference is used when all two attribute values are optional and may be nil
Only one of the two attributes is an optional type, and may be nil when using a non-primary reference
Two attributes must have a value to use a non-primary reference and implicitly expand an optional property.

Forcing a package to be torn

Declaring a forced unpacking of an optional type: var s = String? var t = s!

Extended

Extension SomeType = {}

Agreement

Protocol Someprotocol {}

Mutation method

Methods that can modify instance types inside a method or function are called mutation methods. Mutating

Original address: https://developer.apple.com/library/ios/documentation/swift/conceptual/swift_programming_language/index.html

Address: http://numbbbbb.gitbooks.io/-the-swift-programming-language-/content/

The-swift-programming-language Study Notes

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.