Let declares constants, Var declares variables
Create a button
var btn = UIButton ()
Btn.frame = CGRectMake (100,100,100,100)
Btn.backgroundcolor = Uicolor.redcolor ()
Self.view.addSubview (BTN)
Create an image
var ImageView = Uiimageview ()
Imageview.image = UIImage (name: "1.png")
Imageview.frame = CGRectMake (100,100,100,100)
Self.view.addSubview (ImageView)
String processing
var str1 = "123"
var str2 = "456"
var str = str1 + str2
var age = 10
var hang = 2
var str = "I has \ (hand) hands,\ (age) years"
String Strong turn
var age = 10
var str = String (age)
var str = "\ (age)"
Type
var a:int = 10
Let b:string = "123"
Let a:float = 3.14
Let A = 3.14
Representation of numbers
Let Money = 1000_0000
Let Money = 1_000_000
Type aliases
Typealias myInt = Int
Operator
Range Operator: < ...
For I in 0..<5
{
0..<5 is [0,5] ... i.e. [0,5]
println (i) I is 0,1,2,3,4
}
Overflow operator &+ &-&* &/&%
The copy operator in Swift does not have a return value to avoid the use of = and = = Errors
Tuple assignment
Let (x, y) = (on)
var point = (x:10,y:20)
Accessing tuple elements
Point.x Point.y
point.0 Point.1
Assign value
Point.x = 30
Explicit element type
var person: (int,string) = (20. ") 123 ")
Ignore the value of an element by using an underscore
var person = ("Jack")
var (_,name) = person
For loop
Use the variable i
For I in 1...4{
println (i)
}
No variable i
For _ in 1...4{
println ("* * * *")
}
Switch statement in Swift does not have to be added break
When executing a statement in the same situation
var Scroe = 95
Switch score/10{
case9,10:
println ("excellent")
Case 6,7,8,9:
println ("Pass")
Default
println ("fail")
}
When the condition of execution is a range
Switch score{
Case 90...100:
println ("excellent")
Case 60...89:
println ("Pass")
Default
println ("fail")
}
Variables cannot be used after Fallthrough
Ferrari Throug equivalent to the C language in the switch case to remove the break statement without interrupting execution
, continue execution of the following statement
Label labels a loop that can be directly found in the loop
Out:for _ in 1...2{
For _ in 1...3{
println ("Do Push-ups")
Break out
}
println ("Rest")
S
Function
Format: Func function name (formal parameter list), return value type {
function Body ...
}
Parameter list: formal parameter name 1: formal parameter type, parameter Name 2: formal parameter type
Func sum (num1:int,num2:int), Int {
return NUM1 + num2
}
The return value is empty as a representation: void empty tuple () is not written directly
Returns a tuple type
Func Find (Id:int), (Name:int,age:int) {
Return ("Jack", 20)
}
var p = p.find (2)
println ("name =/(p.0), age =/(p.1)")
External parameters
First: External parameters: Add an external parameter name in front of the formal parameter
Func sum (sum_num1 num1:int, sum_num2 num2:int), Int
{
return NUM1 + num2
}
SUM (sum_num1:20,sum_num2:30)
The second type: Add a # number directly before the formal parameter
Func sum (#num1: int, #num2: int), int
{
return NUM1 + num2
}
SUM (num1:20,num2:30)
Default parameters
Func sum (name:string,age:int = 20)
{
function Body ...
}
SUM ("Jack", age:30)
Default parameter value: A formal parameter with default parameters, Swift automatically generates a
Name of the same external parameter
Add an underscore to the front to cancel the external parameter, underlining the cancellation of everything
Func sum (name:string,_age:int = 20)
{
function Body ...
}
SUM ("Jack", 30)
Variable parameters
Note: A function can have only one variable parameter
The variable parameter must be placed in the last position in the parameter table
Func function (number:double ...)-double{
}
Constants and Variable parameters
Constant parameters: By default, the parameters of a function are constant parameters and cannot be modified.
IE (let name:string)
Variable parameter: Add var to variable before variable
Func sum (var name:string,_age:int = 20)
{
function Body ...
var name = "Jack"
}
There is no function pointer in swift, but you can use the input and output parameters to modify the outside of the function
The value of the part variable, and the input and output parameter is preceded by a inout
Func Change (inout a:int) {
num = 10
}
var a = 20
Change (&a)//is similar to changing the address of variable a, not actually.
println (a) = 10
InOut and Var, let can not be used at the same time
function type (is a data type, like a C-language function pointer, block in OC)
In three steps: 1, define the function. 2. Declare a function type variable or constant. 3, to the function type variable
Assign value
1. Defining functions
Func addtwoints (a:int,b:int), Int {
Return a+b
}
2. declaring function type variables
var mathfunction: (int,int)-Int
3. Assigning values to function type variables
Mathfunction = addtwoints
4. Use
Mathfunction (2,3)
function type as parameter
Func result (mathfunction: (Int,int), int,a:int,b:int), int{
}
function type as return value
Define two functions
Func First (a:int), Int {
Return a
}
Func second (b:int), Int {
Return b
}
function as return value
Func Choose (true:bool)-(int)-int {
Return True?first:second
}
var true = 3
Let Choosefunction = Choose (True > 0)
println (choosefunction) output is the function name
println (Choosefunction (10)) Output results
Closure function
General form: {(parameters)-ReturnType in
Statements
}
Let names = ["123", "456", "789"]
Do not use closures:
Func Backwards (s1:string,s2:string), bool{
return s1 > S2
}
var reversed = sort (names,backwards) sort () Swift standard library function
println (Reversed)
Using closures
Reversed = sort (names,{(s1:string,s2:string), Bool in
return s1 > S2
})
Simplification (judging type by context)
Reversed = sort (names,{s1,s2 in return s1 > s2})
Re-simplification (closed Baoyin return)
If the closure has only one expression, return returns
Reversed = sort (names,{s1,s2 in S1 > s2})
Abbreviated parameter name
$ $ and $ = parameters for the first and second string types in a closure
Reversed = sort (names,{$0>$1})
operator functions
Swift's string type defines the strings implementation for the greater than sign >
Reversed = sort (names, >)
Trailing closures
If you need to put a long closure expression (so that it cannot be written on one line) as the last
Arguments to functions, you can use a trailing closure to enhance the readability of the function, and the trailing closure is a
A closure expression that is written after the function brackets, and the function supports calling it as the last argument
Reversed = sort (names) {$ > $}
Capturing values
1. Closures can capture constants or variables in the context in which they are defined, even if you define these constants and
The original domain of the variable already does not exist, and the closure can still reference and modify these values in the closure function body
2, Swift's simplest form of closure is a nested function, which is defined in the function body of other functions
The function. Nested functions can capture all parameters of their external functions, as well as defined constants and variables
Func makeincrementor (Forincrementor amount:int), Int {
var runningtotal = 0
The Incrementor function does not get any arguments, but Incrementor captures
A reference to the current runningtotal variable, capturing a reference to ensure that when the Makeincrementor
The end time does not disappear, also guarantees the next time executes the Incrementor function,
RunningTotal can continue to increase
Func incrementor (), Int {
RunningTotal + = Amount
Return RunningTotal
}
Return Incrementor
}
Defines a constant called Incrementbyten, which points to an increase in each invocation
Add 10 to the Incrementor function
Let Incrementbyten = Makeincrementor (forincrementor:10)
println (Incrementbyten ()) results are 10
println (Incrementbyten ()) results are 20
println (Incrementbyten ()) results are 30
Swift learns a