Swift Study Notes 2-Set

Source: Internet
Author: User

// ==================================
// 2014/7/21 27 swift collection
// ==================================

Swift provides two set types: arrays and dictionaryies. Both sets are variable and can be added, deleted, and modified after the set is declared.
1. Array
Arrays are defined in the same way as Java arrays, but Swift arrays provide more flexible creation and operation methods.

Array creation and initialization methods:

VaR array1: array <t> = [val1, val2, val3] var array2: T [] = [val1, val2, val3] // common

Create an empty array

var arrays = Array<T>() var arrays = T[]()

Create an array of repeated values

VaR arrays = array (count: 3, repeatevalue: 0.0) // The Compiler automatically recognizes the value type var arrays = T [] (count: 3, repeatevalue: 0.0)

Common functions and attributes:

Array2.count (-> INT) // number of output elements array2.isempty (-> bool) // checks whether the array is an empty array array2.append (val4) // append the array element array2 + = val4 // append the array element array2 + = [val5, val6] // append an array element array2 [0] = val7 // modify the value of array2 [4... 6] = [val5, val6] // returns the subscript 4 ~ 6, replace with the corresponding element, so that the number of elements in the array may be reduced or add array2.insert (val7, atindex: 0) // Add new elements in the specified subscript, automatically shifts array2.removeatindex (0) backward. // removes the element at the specified position and returns this element. Array2.removelast () // remove the last element and return the calcium element.

Array traversal is similar to Java. For syntax, use the swift syntax.

for item in array2 {      println("\(item)") }

Returns the values of the array subscript and subscript. The enumerate global function is used.

for (index,value) in enumerate(array2) {      println("item \(index + 1): \(value)") }

2. Data Dictionary)
Here, the data field is a set of values in the form of key-value.

VaR dicts = ["cls1": "Java", "cls2": "C ++", "cls3 ": "Swift"] dicts ["cls3"] // output swift dicts ["cls3"] = nil // "cls3 ": "Swift" deletes dicts ["cls3"] = "C" // modifies dicts ["cls4"] = "Swift" // adds a new pair of dicts. removevalueforkey ["cls4"] // Delete the key value and return the value

Data Dictionary provides key-value traversal, key traversal, and value traversal.

for (key, val) in dicts {      println("\(key) : \(val)") } for key in dicts.keys {      println("\(key)") } for val in dicts.values {      println("\(val)") }

Empty data dictionary instantiation

VaR namesofintegers = [INT: String] () namesofintegers = [:] // clear the dictionary

The key type of the data dictionary. It must be the type that can calculate the hash value.
If a = B, there is a. hashvalue = B. hashvalue

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.