Swift collection type

Source: Internet
Author: User

The swift language provides array and dictionary Collection types.

 

 The data value types stored in arrays and dictionaries in SWIFT must be clear, that is, Arrays can only store data of the same type.

1: Array

Array Creation

VaR shoppinglist: String [] = ["tset", "hell", "hell"] var arrayint: int [] = [1, 3]

VaR arrayintex = [55, 32] // use the inference type

// Create an empty array

VaR someints = int [] ()
Println ("\ (someints. isempty )")
// Create a specified type (double) and number (3) and set the initial value (0.0)
VaR threedoubles = double [] (count: 3, repeatedvalue: 0.0)

Use subscript to access the array. The subscript starts from 0. Be sure not to cross-border the subscript.

var arrayIntEx  = [1, 3]arrayIntEx[1] = 99println("\(arrayIntEx[1])")

The modifier of the array depends on VaR or let control.

Count and isempty are used to check the number of array elements.

var arrayIntEx  = [1, 3]let count = arrayIntEx.countlet bFlag = shoppingList.isEmptyprintln("\(count)\n\(bFlag)")

Append an array element. It must be a var array, and the appended element type must be the same as the array type. You can also use the + = operation.

var arrayIntEx  = [1, 3]arrayIntEx.append(8)arrayIntEx += 9arrayIntEx += [55, 66]println("\(arrayIntEx[2])\n\(arrayIntEx[3])\n\(arrayIntEx[4])\n\(arrayIntEx[5])\n")

When an array element specifies the index number to insert (). Insert specifies the index number, the maximum value is the number of original elements + the number of currently inserted elements-1, which is similar to adding at the end. After insert, the original elements are moved.

var arrayIntEx  = [1, 3]arrayIntEx.insert(5, atIndex:2)println("\(arrayIntEx)")

Array specified index number Delete removeatindex

var arrayIntEx  = [1, 3, 9]arrayIntEx.removeAtIndex(1)println("\(arrayIntEx)")

 

For in for Array Traversal

var arrayIntEx  = [1, 3, 9]for item in arrayIntEx {    println("\(item)")}

If you want to use the index number and value, use enumerate

var arrayTest = ["ax", "bx", "sx", "sxx"]for (index, value) in enumerate(arrayTest) {  println("Item: \(index + 1): \(value)") }

Array concatenation if the two arrays are of the same type, you can splice them with +. Of course, the order of the results is the same as that when you splice them.

var arrayIntFirst = [2, 5]var  arrayIntSecond = [6, 77]var arrayRes =  arrayIntSecond +  arrayIntFirstprintln("\(arrayRes)")

 

2: Dictionary

When using the swift dictionary, you must specify the key and value types that can be stored. There is no sequence in the dictionary storage. It depends on a keyValue that can be hashed for search. It is estimated that the keyValue is hashed and used as the red-black binary tree created by the key to find the key.

The dictionary uses the dictionary <keytype, valuetype> definition. The keytype must be hashable, And the valuetype type must be consistent in a dictionary.

Create dictionary

VaR airports: dictionary <int, string> = [1: "yamide", 2: "Soga"] // display type description var dicdefault = [2: "XX", 33: "oo"] // type inference is used. In this case, 2.0 and 3 cannot be used, and type conversion inference is not supported.
VaR namesofintegers = dictionary <int, string> () // create an empty array
VaR dicempty = [:]

Whether the dictionary can be modified depends on VaR and let control.

Use the Count attribute to obtain the number of dictionary elements.

var dicDefault = [2:"xx", 33:"oo"]println("\(dicDefault.count)")

Update of dictionary elements. You can use the subscript keyword to add

var dicDefault = [2:"xx", 33:"oo"]dicDefault[22] = "tt"println("\(dicDefault)")

If the key added by the subscript is already in the dictionary, it will update the data.

The updatevalue (forkey :) method can be used to set or update the value corresponding to a specific key. If the keyValue is not added, it will be updated if it already exists in the dictionary.

 

var dicDefault = [1:"xx", 2:"oo"]dicDefault[3] = "tt"dicDefault.updateValue("sx", forKey:4)//adddicDefault.updateValue("sbcd", forKey:2)//update

Delete removevalueforkey

var dicDefault = [1:"xx", 2:"oo"]dicDefault[3] = "tt"dicDefault.updateValue("sx", forKey:4)dicDefault.removeValueForKey(2)println("\(dicDefault)")

You can use for in to traverse dictionary key-value pairs. Returned in the form of tuples

var dicDefault = [1:"xx", 2:"oo"]for(key, value)in dicDefault {    println("key:\(key) value:\(value)\n")}

The dictionary can use values to return all values. Keys to return all keywords.

var dicDefault = [1:"xx", 2:"oo"]var keys = dicDefault.keysvar values = dicDefault.valuesfor key in keys {    println("key:\(key)\n")}for value in values {    println("value:\(value)\n")}

 

 

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.