设为首页 加入收藏

TOP

Swift3 - String 字符串、Array 数组、Dictionary 字典的使用(二)
2017-10-10 12:15:07 】 浏览:8504
Tags:Swift3 String 字符串 Array 数组 Dictionary 字典 使用
(
"arr1数组是空") }else { print("arr1数组不空") } // arr1[arr1.count-2] 取数组的某个元素 print(arr1[arr1.count-2])// hySwift // print(arr1[0])// hyArr // public var first: Self.Iterator.Element? { get } print(arr1.first!)// hyArr // 遍历数组 for i in 0..<arr1.count { print(arr1[i]) } // 包含 if arr0 .contains("test") { print("数组包含 test") }else { print("数组不包含 test") } // 删除元素 // arr2 .remove(at: 4) // arr2 .removeSubrange(1..<3)// 删除 1、2 两个元素 // arr2 .removeLast() // arr2 .removeFirst() arr2 .removeAll() print(arr2) // 添加元素 arr2 .append("new1")// ["new1"] arr2.append(contentsOf: ["Shakia", "William"]) print(arr2) arr2 = arr1 + arr2// ["hyArr", 1, "hySwift", 3, "new1"] arr2 = arr1 arr2 .insert("insertElement", at: 3)//["hyArr", 1, "hySwift", "insertElement", 3, "new1"] // 更换 if let i = arr0.index(of: "test") { arr0[i] = "测试" } arr2[0] = "domy" print(arr2) // 数组排序 var sortArr = [3,5,1,0,8,0] sortArr.sort(by: >) print(String(format:"排序后:"),sortArr)// 排序后: [8, 5, 3, 1, 0, 0] // 二维数组 let tArr1 = [["tSwift","haha"],1,[3,2]] as [Any] let subArr1 = tArr1[0] print(subArr1) /// Array => NSArray /// 苹果的例子 /// Description: /// The following example shows how you can bridge an `Array` instance to /// `NSArray` to use the `write(to:atomically:)` method. In this example, the /// `colors` array can be bridged to `NSArray` because its `String` elements /// bridge to `NSString`. The compiler prevents bridging the `moreColors` /// array, on the other hand, because its `Element` type is /// `Optional<String>`, which does *not* bridge to a Foundation type. let colors = ["periwinkle", "rose", "moss"] let moreColors: [String?] = ["ochre", "pine"] let url = NSURL(fileURLWithPath: "names.plist") (colors as NSArray).write(to: url as URL, atomically: true) // true (moreColors as NSArray).write(to: url as URL, atomically: true) // error: cannot convert value of type '[String?]' to type 'NSArray' /// Array 的更多其他用法点进去查看方法文档 }

3、字典的简单使用

    // 字典 dictionary
    func dictionaryTest() -> Void {        
        // 创建字典
        var dict = [200:"ok",400:"error"]// [key:value]
        var emptyDict: [String: Any] = [:]// 空字典 var emptyDict: [Int: String] = [:]
        emptyDict = ["key1":"value1","key2":2]
        
        // Getting and Setting Dictionary Values
        print(dict[200]!)// ok
        print(emptyDict["key1"]!)// value1
        // 添加键值对
        emptyDict["key3"] = "value3"
        print(emptyDict)// ["key2": 2, "key3": "value3", "key1": "value1"]
        // 更新键值对的value
        emptyDict["key2"] = "updateva lue2"
        print(String(format:("更换value后:")),emptyDict)
        
        
        var interestingNumbers = ["primes": [2, 3, 5, 7, 11, 13, 15],
                                  "triangular": [1, 3, 6, 10, 15, 21, 28],
                                  "hexagonal": [1, 6, 15, 28, 45, 66, 91]]
        //  排序
        for key in interestingNumbers.keys {
            interestingNumbers[key]?.sort(by: >)
        }
        print(interestingNumbers["primes"]!)
        /// print(interestingNumbers)
        /// ["hexago
首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Swift3新特性汇总 下一篇swift 学习笔记[1]

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目