Simple syntax for Swift's entry (III.)

Source: Internet
Author: User

Collection array
    • The array uses the [] definition, which is the same as OC
//: [Int]let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    • Traverse
for num in numbers {    print(num)}
    • Get the specified item content by subscript
let num1 = numbers[0]let num2 = numbers[1]
    • Variable & non-volatile
      • letDefining immutable Groups
      • varDefining a mutable array
let array = ["zhangsan", "lisi"]//: 不能向不可变数组中追加内容//array.append("wangwu")var array1 = ["zhangsan", "lisi"]//: 向可变数组中追加内容array1.append("wangwu")
    • Types of arrays
      • If all content types are consistent during initialization, the content of that type is saved in the optional array
      • If all content types are inconsistent during initialization, the selected array holds theNSObject
//: array1 仅允许追加 String 类型的值//array1.append(18)var array2 = ["zhangsan", 18]//: 在 Swift 中,数字可以直接添加到集合,不需要再转换成 `NSNumber`array2.append(100)//: 在 Swift 中,如果将结构体对象添加到集合,仍然需要转换成 `NSValue`array2.append(NSValue(CGPoint: CGPoint(x: 10, y: 10)))
    • Definition and instantiation of arrays
      • Use : to define only the type of an array
      • Add value is not allowed before instantiation
      • Use [类型]() the ability to instantiate an empty array
var array3: [String]//: 实例化之前不允许添加值//array3.append("laowang")//: 实例化一个空的数组array3 = [String]()array3.append("laowang")
    • Merging of arrays
      • Must be an array of the same type to be able to merge
      • In development, the types of objects that are normally stored in arrays are the same!
array3 += array1//: 必须是相同类型的数组才能够合并,以下两句代码都是不允许的//array3 += array2//array2 += array3
    • Deletion of arrays
//: 删除指定位置的元素array3.removeAtIndex(3)//: 清空数组array3.removeAll()
    • Memory allocation
      • If you append an element to an array that exceeds the capacity, it is directly based on the existing capacity * 2
var list = [Int]()for i in 0...16 { list.append(i) print("添加 \(i) 容量 \(list.capacity)")}
Dictionary
    • Defined
      • Use the same [] definition dictionary
      • letImmutable dictionaries
      • varVariable dictionaries
      • [String : NSObject]Is the most commonly used dictionary type
//: [String : NSObject] 是最常用的字典类型var dict = ["name": "zhangsan", "age": 18]
    • Assign value
      • Assigning direct use of dict[key] = value formatting
      • If key does not exist, the new value is set
      • If key exists, the existing value is overwritten
//: * 如果 key 不存在,会设置新值dict["title"] = "boss"//: * 如果 key 存在,会覆盖现有值dict["name"] = "lisi"dict
    • Traverse
      • k, v can write casually
      • The front iskey
      • The Back isvalue
//: 遍历for (k, v) in dict {    print("\(k) ~~~ \(v)")}
    • Merging dictionaries
      • If key does not exist, a new value will be established, otherwise the existing value will be overwritten
//: 合并字典var dict1 = [String: NSObject]()dict1["nickname"] = "大老虎"dict1["age"] = 100//: 如果 key 不存在,会建立新值,否则会覆盖现有值for (k, v) in dict1 { dict[k] = v}print(dict)

Simple syntax for Swift's entry (III.)

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.