The-swift-programming-language learning notes, swiftlanguage

Source: Internet
Author: User

The-swift-programming-language learning notes, swiftlanguage
Constants and variables

Constants cannot be modified. constants defined in classes can be assigned values in constructors. Let Modifier
Variables can be modified. Var Modifier
Traversal of characters in a string
For code in string {}
For codeunit in string. unicodeScalars {}

Control statement

Break jump out of the loop body
Continue terminates the current instance and enters the next cycle
Use label to configure break and continue. Use label to mark the loop body. The break label jumps out of the marked loop body. The continue label is the cycle for entering the next mark.
Fallthrogh: Used in the switch statement. It 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 break.

Scope of use

Number1... Number2 = number1 <= var <= number2
Number1. <number2 = number1 <= var <number2

Tuples

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 copying a dictionary, it depends on whether the dictionary value is a value type or a reference type.

Function

Func Function (params)-> params {method body}
Function parameters are let Constants by default. variables need to be defined in parameters that define var
When defining a parameter, you can assign the default value func Test (paramens: String = "default value "){}
Variable Parameter func sum (numbers: Double ...) Similar to passing an Array <T> Parameter
Parameter transfer reference inout keyword
Returns multiple values (returns a value of the tuples)
The function itself can be passed as a parameter or a range value.
Internal name and external name of the Parameter

Function Type nested Functions

Define functions in other function bodies

Closure

Similar to lambdas expressions
A global function has a name but does not capture any worthwhile closure.
A nested function is a closure with a name and can capture its closed function domain.
A closure expression is a closure written in lightweight syntax that can capture variables or constants in its context without a name.
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 function parameter
Sort (numbers, backwars)
Closure expression transfer
Sort (numbers ,{
(S1: String, s2: String)-> bool in
Return s1> s2
})
You can write a row for a short closure.
Sort (numbers, {(s1: String, s2: String)-> bool in return s1> s2 })
The closure can automatically infer the parameter type.
Sort (numbers, {s1, s2 in return s1> s2 })
The return keyword can be omitted in a single line expression.
Sort (numbers, {s1, s2 in s1> s2 })
Abbreviated parameter name
Sort (numbers, $0> $1)
Operator Functions
Sort (numbers,>)
Trailing closure: Pass the closure as the last parameter to the function, and you can use the trailing closure.
Sort (numbers ){
(S1: String, s2: String)-> bool in
Return s1> s2
})
Sort (numbers) {$0> $1}

Structures and Classes

In swift, only similar reference types are supported, while others are value types.

  • Common:

Define attributes
Definition Method
Define subscript
Define the constructor
Extension Method (external)
Protocol Compliance)

  • Different:

Class inheritance allowed
Type conversion allows you to check and interpret the type of a type instance at runtime.
The Destructor allows a class instance to release any allocated resources.
The reference count allows multiple references to a class.

Attribute

You can use closures and functions to set the default attribute values.

Storage Properties

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

Constant and storage attributes

Define the struct attribute of a constant, and all the attributes of the struct become constants. The attributes of a referenced class are different. After a class instance is assigned to a constant, you can still modify the variable attributes of an instance.

Delayed Storage attributes

When @ lazy is used to represent a Delayed Storage attribute, the delayed storage attribute must be declared with var. When the value of the attribute is dependent on the external factors of the specific value before the instance construction process, or when the attribute value requires a lot of complicated calculations, it can be calculated only when needed.

Calculation attribute

The calculated attribute does not directly store the value, but provides a getter to get the value, and a setter to indirectly set the value of other attributes or variables. In setter, newValue is used by default to indicate the received value.

Read-Only computing attributes

That is, only the calculation attribute of getter is used.

Property listener

WillSet is called before new values are set. NewValue indicates the new value by default.
DidSet is called immediately after the new value is set. OldValue is used by default to indicate the old value.

Global and local variables

The pattern to which the calculation property and property listener belong can also be used for global variables and local variables. global variables are variables defined outside functions, methods, closures, or any type, local variables are defined in functions, methods, or closures.
Global constants or variables are computed with delay. They are similar to the delay calculation attribute. The difference is that global variables or constants do not need to be marked with the @ lazy attribute; constants or variables in the local range do not delay calculation.

Type property

The Type attribute defines the data shared by all instances of a specific type.
For a value-type class (enumeration and structure), you can define storage and computing-type attributes. For a class, you can only define computing-type attributes.
Declare a type attribute with static in enumeration and organization
Declare a type attribute with class in class

Method

The Value Type attribute cannot be modified in its strength method. If you want to modify the attribute of a specific value type, you can select the mutating method ), then, the method can modify its attributes from inside the method.

Type Method

Same as type property

Subscript

Syntax:
Subscript (parameter: parametertype)-> returntype {
Get {
}
Set (newValue ){
}
}

Constructor

Variables or constant attributes defined in classes and structures must be assigned values in the constructor, or assigned values during definition.
You can modify constant attributes in the constructor.

Optional attribute type

Use type? Statement. The default value is nil.

Automatic Reference count (ARC)

Swift uses ARC to track and manage the memory used by applications. In most cases, it means that in swift, memory management still works and you do not need to consider memory management. When the instance is no longer used, ARC will automatically release the memory occupied by the class instance.
For solutions to class instances, see strong reference ring.
Weak reference (use weak to declare if referenced)
No primary reference (use unowned to declare a primary reference)
Optional attributes of no primary reference and implicit expansion (type of the optional attribute of implicit expansion !)
Strong ring reference generated by the closure: assign a closure to an attribute of the class instance, and this closure uses an instance. This closure may access a certain attribute of the instance, such as self. someProperty, or call a method, such as self. someMethod ().
Solve the strong ring reference generated by closure and define the owner list (declared using [unowed/weak self)
@ Lazy var property: (parametertype)-> returntype = {
[Unowned self] (parameter: parametertype)-> returntype in
}
When the closure and the occupied instance are always referenced and destroyed at the same time, the closure possession is defined as unowned. On the contrary, when possession references may sometimes say nil, define the possession in the closure as weak. Weak references are always optional.
When both attribute values are optional and both types may be nil, use weak references.
Only one of the two attributes is of the optional type, and nil may be used when no primary reference is used.
If both attributes must have values, the optional attributes can be expanded both with no primary reference and implicitly.

Forced package splitting

Declare an optional type of forced unpacking: var s = String? Var t = s!

Extension

Extension SomeType = {}

Protocol

Protocol SomeProtocol {}

Mutation Method

The method that can modify the instance type within a method or function is called a mutation method. Mutating

 

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

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

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.