Swift Basic syntax

Source: Internet
Author: User

1. Automatic type inference for constant volume

Let-age = 20 constant immutable

var num = 24 variable

num =

Let count:Int = 2

Double(count) type conversions

2. String

Swift's string operation is more complex and often turns into OC nsstring

Let name:String = "Hrrrrrrrrr"

var addr:string = string()  

Print(name)

Print("My name is \(name)")  

Name. EndIndex

(name as nsstring). Substringtoindex(5)

(name as nsstring). Substringwithrange(nsmakerange(5, 2))

(name as nsstring). Length

3. The array can only hold one type, and OC is not the same

var heightarray:array<Int> = array()

Heightarray. Append(3)

Heightarray. Insert(2, Atindex: 1)

HEIGHTARRAY[0]

4. Dictionaries

Let numberoflegs:[String:Int] = ["Cat":4,"Duck":2]

5.bool type True False

6. Optional value optional

var temp:String? Nil

Temp = "Test " "Test"

Print(temp) "Optional (" test ") \ n"

Automatically add a question mark to indicate that temp exists, exists to do, does not exist, does not

Print(temp?. endIndex) "Optional (4) \ n"

var temp:string! Must have

7.for in loop

For index in 1... 5{

Print(index)

}

For name in namesarray{

print(name)

}

Let numberoflegs:[String:Int] = ["Cat":4,"Duck":2]

For (Name,legs) in numberoflegs{

  One pair to take out

}

Swift Basic syntax

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.