Swift Learning Memorandum

Source: Internet
Author: User

1, the definition of constants

Constant definitions use the Let keyword, which can take one of the following two ways:

1) Standard mode initialization

Let Cpu_num:int = 2

2) for the exact value, you can use type inference, such as let Cpu_num = 2

Constants are usually initialized at the time of definition, in order to be concise and generally use the 2nd method above.

Constants can be declared but not initialized, and then initialized in a specific situation, but only once. Because there is no specific value at the time of declaration, no exact type inference is possible, so it is necessary to declare it in the standard way, plus the type value.

such as: let Cpu_num:int; Cpu_num = 2

2, the definition of variables

Variable definitions use the var keyword, which can take the form of:

1) Standard mode

var welcome:string = "Hello world!"

2) Type inference

var Welcome = "Hello world!"

can also be declared only, such as Var welcome:string

3, meta-group

Let Http404error = (404, "Not Found")

var (statusCode, StatusMessage) = Http404error

var (_, result) = Http404error

You can also specify a name for each field when you define a tuple, such as:

Let Http200status = (statuscode:200, Content: "OK")

This can be used when printing:

Print(http200status. StatusCode, http200status. Content)

4. Optional type

You can define a type as an optional type, for a variable of this type, it can have a specific value, or it can have no value, or nil if there is no value

Such as:

Let ischeckid: Bool? = true

If ischeckid = = Nil {

Print("No need check ID")

} else {

Print("Check ID")

}

5. Arrays

There are several ways to define an array:

1) You can declare it first and initialize it at the appropriate time.

VAR score: [Int]; Score = [Int] ()

2) You can define an empty array directly

var score = [Int] ()

3) defines an array with an initial size and class with the specified value.

var score = [Int] (count: ten, Repeatedvalue: 0)

4) define an array with an initial value

var score = [90, 59, 85.5]

Insert an element into the array, assuming that the following array is defined:

var score = [Int] ()

You can add elements to an array in the following ways:

Score. Append(ten)

Score. Append()

Score. Append(+)

Print (score)

Output

[10, 20, 30]

The array subscript method can only be used to query or modify an existing element and cannot be used to insert a new element. The following code will issue a run-time error:

SCORE[3] = 100

However, you can modify an existing element:

SCORE[2] = 100

Output

[10, 20, 100]

You can also insert a new element in the middle of an existing element:

Score. Insert(atindex: 0)

Output

[100, 0, 20, 100]

You can also insert a new element based on the size of the current array, at which point the Insert function is equivalent to append

Score. Insert(atindex:4)

Throwing a run-time exception

Score. Insert(atindex:20)

6. Collection

A collection is used to hold a worthy container with the same data type, and the data in the collection cannot be duplicated.

Definition of the collection:

1) var cities:set<string> = ["Beijing", "Shanghai", "Shenzhen"]

2) var cities:set = ["Beijing", "Shanghai", "Shenzhen"]//based on type inference

The order of elements in set is nondeterministic, that is, the time-elapsed printing does not necessarily coincide with the order in which the elements are declared or inserted.

Print (Cities)

Output

["Shanghai", "Shenzhen", "Beijing"]

7. Dictionaries

The dictionary belongs to the associative container, which is a collection of <key-value> key-value pairs where key cannot be duplicated.

Definition of Dictionary:

1) var score: [String:int] = ["Jack": +, "Aaron": 100]

2) var score = ["Jack": +, "Aaron": 100]

3) var score = dictionary<string, int> ()

Operation of the Dictionary:

1) access to an element

The value of the dictionary is optional, and if accessed directly by subscript, it may not be the same as what you expected, such as

Print (score["Jack"])

Output

Optional (90)

However, if you can make a normal comparison by value, such as if score["Jack" = = 90

If you want to print the values normally, you can:

If let Jackscore = score["Jack"] {

Print (Jackscore)

}

Output

90

Traverse Dictionary

For (name, score) in score {

Print (name, score)

}

Prints a value for a nonexistent key that will return nil

Dictionary and set, shun with uncertainty

2) inserting data

score["Merry"] = 80

3) Modify the data

Score. updatevalue(forkey: "Merry")

Updatevalue a nonexistent key, which is equivalent to adding a new element

Swift Learning Memorandum

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.