It’s long!! If you really want to learn, or find a quiet evening, slowly read, slowly experience the beauty of Swift, here is my Github demo:Github.com/tanzhiwen/S…

Welcome to study under guidance!!



directory

01- Variables and constants

02 – operator

03 – optional

04- Conditional statements

05 – cycle

06 – string

07 – tuples

08 – array

09 – dictionary

10- Objects and classes

11 – the enumeration

12 – attributes

13- Subscript script





01- Variables and constants

1.1 Basic data types

1. The integer: Int

2. Floating point numbers: Double represents 64-bit floating point numbers and Float represents 32-bit floating point numbers

3. Boolean type: Bool. Boolean values are true and false

4. String: String

5. Character

1.2 Variables and Constants

1. Variables: Values can be modified,var modified

2. Constant: values cannot be modified,let modifier

var a =20

a =10

let b =20

Error :’b’ is a ‘let’ constant

1. Automatically derive the properties of declared variables or constants

2. Use the Option + click key to view the property type

// 1. Automatically derive types

let str =”ningcol”

let intValue =10

Let floatValue = 1.2

// 2. Specify the data type

let doubleValue:Double =10

02 – operator

1 Basic operator

var a = 5

let b = 3

// 1. Assignment operator

let c = b

// 2. Add, subtract, multiply and divide

1 + 2

5-3

2 * 3

10.0/2.5

2 Implicit conversions are not made in any case and must be computed in the same type

let num1 =1

Let num2 = 2.2

let num3 = Double(num1) + num2


An explicit type conversion is required

Let j = 2.2

Let I: Float = 1.2

i + Float(j)

4. Compute complements

a % b

5 minus

let minusB = -b

6. Combinatorial assignment operation

a +=2

7. Comparison operation

1 = = 1

2! = 1

2 > 1

1 < 2

1 > = 1

2 < = 1

8. Triadic operation

let d = a > b ? A 100-200


9. Null conjunction operation

1. The null conjunction operator (a?? B) Null judgment will be performed on optional type A (see 04- Optional)

2. If aName is nil, execute?? Otherwise, execute aName. Spaces on both sides)

var aName: String? =”ningcol”

//var aName: String? = nil

let bName = aName ??” aNameIsNil”

10. Interval arithmetic

1. Closed interval operator (a… B) Define an interval containing all values from a to B, including both a and b

2. Half-open interval (a..

for index in 1… 5 {

print(index)

}

for index in 1.. < 5 {

Print (” half-open :\(index)”)

}

11. Logical operations

1. Logic is not (! A): The Boolean value is reversed

2. Logic and (a && b): The entire expression will be true only if both a and b are true

3. The logic or (a | | b) : one of two logical expressions for tru e, the entire expression is true

let allowedEntry =false

let enteredDoorCode =true

if ! allowedEntry {

print(“ACCESS DENIED”)

}

if allowedEntry && enteredDoorCode {

print(“Welcome!” )

}else{

print(“ACCESS DENIED”)

}

if allowedEntry || enteredDoorCode {

print(“Welcome!” )

}else{

print(“ACCESS DENIED”)

}


03 – optional

1. An optional value

Optional values: Can have values, can be nil(with? Represents optional value)

// The URL is optional

let URL = NSURL(string:”http://www.baidu.com/”)

// STR is optional

var str: String? =”ningcol”

The // var option defaults to nil

var a:Int?

print(a)

// if let: make sure myUrl has a value before the branch is entered

iflet myUrl = URL{

print(myUrl)

}

var aName: String? =”ningcol”

// var aName: String? = nil

var aAge: Int? = 18

iflet name = aName,let age = aAge {

print(name + String(age))

}

// The value can be modified

ifvar name = aName,let age = aAge {

name =”lisi”

print(name + String(age))

}

3.guard let

1. Guard let is opposite to if let. It must have a value, and returns no value

2. Lower the branch hierarchy

3. We can’t show the effect on playground. We show it in functions

// Create a class (see :10- Objects and classes)

classtest{

func demo(){

let aNick: String? =”ningcol”

let aAge: Int? = 10

guard let nick = aNick ,let age = aAgeelse{

print(“nil”)

return

}

print(“guard let: “+ nick + String(age))

}

}

var t = test()

t.demo()

4. Forcibly unpack the packet

// Create an array (see :08- group number)

var dataList:[String]?

dataList = [“zhangsan”,”lisi”]

/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

1.dataList? 表示 datalist 可能为 nil

2. If it is nil,.count does not return nil

2. If not nil,.count is executed to return the number of elements in the array

4.?? Null conjunction operator (see the 02- operator)

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /

let count = dataList? .count ?? 0

// Datalist must have a value, otherwise it will fail!

let cou = dataList! .count

04- Conditional statements

4.1. If statements

/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

1. Curly braces are mandatory

2. There is no “neither zero nor true”, only true/false

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /

let num =20

ifnum >10{

Print (” more than 10 “);

}else{

Print (less than or equal to 10)

}

4.2 the switch

/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

1. Values can be of any type

2. Scope is only inside the case

3. Don’t break

4. Every case should have a code

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /

let name =”nick”

switchname {

case”nick”:

let age =18

print(“one\(age)”)

case”fil”:

print(“two”)

case”Davi”:

print(“three”)

Case “”:break// a line of code

case”tom”,”ningcol”:

print(“tomAndNingcol”)

default:

print(“other”)

}

Switch branch usage scope

let count = 3_000

var naturalThings:String

switchcount{

case0:

NaturalThings =” Number 0″

case1… 3:

NaturalThings =” Numbers 1-3″

case4… 9:

NaturalThings =” Numbers 4-9″

case10… 99:

NaturalThings =” Numbers 10-99″

case1000… 9999:

NaturalThings =” Numbers 1000-9999″

default:

NaturalThings =” Numbers 9999 +”

}

print(naturalThings);

// Output: numbers 1000-9999

5.1 a for loop

// Remove C style loops (.. < interval operator, see :02- budget)

foriin0.. The < 10 {

print(i)

}

Print (“—- step loop —–“)

// Increment (step 2)

foriinstride(from:0, to:12, by:2) {

print(i)

}

Print (” start decrement “)

/ / decline

foriinstride(from:12, to:0, by: -2) {

print(i)

}

Print (“—- reverse loop —-“)

let range =0… 10

// reverse loop

foriinrange.reversed(){

print(i)

}

5.2 Loop Structure while

/ *

The while statement, which breaks only if IP <5 is false

* /

varip = 0

while (ip<5){

print(“ip=\(ip)”)

ip += 1

}

// Run the result

//ip=0

//ip=1

//ip=2

//ip=3

//ip=4

/ *

The repeat-while loop, no matter what pa is, executes once and, at judgment, breaks out of the do while statement if it is false

* /

varpa = 5

repeat{

print(“pa=\(pa)”)

pa += 1

}while (pa<5)

// Run the result

//pa=5

06 – string

1.String structure, which is more efficient than objects, is recommended and supports traversal

2. Nsstrings NSObject inheritance

Var STR :String =”Hello”

//var st:NSString = “hah”

// Number of bytes

print(str.lengthOfBytes(using: .utf8))

// String length

print(str.characters.count)

forainstr.characters{

print(a)

}

// String concatenation

let name:String? = “king”

let age =80

Let location =” next door”

print(location + (name ??” A “) + String(age) +”

//’\(variable name)’ will automatically convert concatenation

Print (” \ (location), (name), age (age) “)

let rect = CGRect(x:0, y:0, width:100, height:100)

print(“\(rect)”)

// Format string

let h =13

let m =5

let s =9

let timeStr = String(format:”%02d:%02d:%02d”, arguments: [h,m,s])

let timeStr1 = String(format:”%02d:%02d:%02d”, h,m,s)

1. Use Range in Swift, preferably change String to NSString

2. Str.substring (with: Range

3. ‘value as type’ is converted as type

(STR as nsstrings.) the substring (with: NSMakeRange (2, 5))

let index = str.index(str.startIndex, offsetBy:3)

str.substring(from: index)

// “123” is only used to fetch the index position

str.substring(from:”123″.endIndex)

print(“****\(str.substring(from: “123”.endIndex))”)

str.substring(to: index)

String to use Range

let myRange = str.startIndex..

str.substring(with: myRange)

let myRange1 = index..

str.substring(with: myRange1)

07 – tuples

// The number of elements in a tuple is fixed and cannot be added or deleted

Var stu = (404,” white “)

// Support nesting

Var MSG = (” 信息”, (” 信息”, 34))

print(stu)

print(msg)

var (a,b) = stu

print(a,b)

// If only individual values in a tuple are needed, use “_” to process unwanted values

let (c,_) = stu

print(c)

// Get the value of a tuple by ordinal

print(“status is \(stu.0)”)

// It can be modified

stu.0=500

Let message = (status:100, MSG :” ha ha “)

print(“message is \(message.status)and \(message.msg)”)


08 – array

8.1 Array Definition

Arrays decorated with lets are immutable tuples

Arrays decorated with var are mutable arrays

// Square brackets [] create the array

let array1 = [“zhangsan”,”lisi”]

Let array2 = [1, 2, 3, 4, 5]

Var array3:[Int]// Define an array (not initialized)

Array3 = [Int]()// Initialize

// Declare an empty array, (must be initialized)

Let array4 = [String](

let array5:[Any] = [“zhangsan”,”lisi”,20]

Var arr3 = [Double] (repeating: 0.0, the count: 3) / / [0.0, 0.0, 0.0]

Var arr4 = Array(Repeating :3.0, Count :3)

var arr: [String] = [“Alex”,”Brian”,”Dave”]

print(arr.count)

print(arr[0])

8.2 Array traversal

/ / forin way

for name in array1{

print(name)

}

// go through the number group

for i in 0..

print(array2[i])

}

// interval traversal

for item in array2[0..<2] {

print(“item\(item)”)

}

// Iterate over both the subscript and the content

Print (“===== traverses both subscript and content =====”)

for e in array2.enumerated(){

print(e)

//offset subscript element value

Print (” tuple \(e.ffset) \(e.lement)”)

}

// the subscript and the value are traversed simultaneously

for(n, s) in array2.enumerated() {

print(n,”===”, s)

}

// reverse order traversal

for a in array2.reversed(){

print(a)

}

// iterate over the subscript and numeric reverse order

for(xxx,ooo)in array2.enumerated().reversed() {

print(xxx,”==”,ooo)

}

To be continued…

If you insist to see here you can go to the door of knowledge ha!!

www.cnblogs.com/ningmengcao…