你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> Swift_Set詳解

Swift_Set詳解

編輯:IOS開發綜合

本文為您介紹Swift_Set詳解的文章,具體方法請看介紹

Swift_Set詳解

點擊查看源碼

初始化
//初始化
fileprivate func testInit() {
    //空Set
    var set = Set<String>()
    print(set)
    set = []
    print(set)
    
    //通過參數設置
    set = Set(["XuBaoAiChiYu","1045214799"])
    print(set)
    
    set = Set(arrayLiteral: "XuBaoAiChiYu","1045214799")
    print(set)
    
    set = Set(set)
    print(set)
    
    let set2: Set<String> = ["XuBaoAiChiYu","1045214799"]
    print(set2)
    
    /* print
     
     []
     []
     ["1045214799", "XuBaoAiChiYu"]
     ["1045214799", "XuBaoAiChiYu"]
     ["1045214799", "XuBaoAiChiYu"]
     ["1045214799", "XuBaoAiChiYu"]
     
     */
}
相關屬性
//相關屬性
fileprivate func testProperties() {
    
    let set: Set<String> = ["IOS","XuBaoAiChiYu","1045214799"]
    print(set)
    
    print("count:\(set.count)") // 有多少個元素
    print("first:\(set.first)") // 頂部元素
    print("isEmpty:\(set.isEmpty)") // 是否為空
    print("hashValue:\(set.hashValue)") // hash值
    
    // 首位和末位
    var index = set.startIndex
    print("index:\(index)")
    
    index = set.endIndex
    print("index:\(index)")
    
    /*  print
     
     ["1045214799", "IOS", "XuBaoAiChiYu"]
     count:3
     first:Optional("1045214799")
     isEmpty:false
     hashValue:7244437487393105174
     index:SetIndex<String>(_value: Swift.SetIndexRepresentation<Swift.String>._native(Swift._NativeSetIndex<Swift.String>(nativeStorage: , offset: 0)))
     index:SetIndex<String>(_value: Swift.SetIndexRepresentation<Swift.String>._native(Swift._NativeSetIndex<Swift.String>(nativeStorage: , offset: 4)))
     
     */
}
查找元素
//查找元素
fileprivate func testFindingObjects() {
    let set: Set<String> = ["IOS","XuBaoAiChiYu","1045214799"]
    print(set)
    
    //是否存在這個元素
    print("contains:\(set.contains("XuBaoAiChiYu"))")
    
    //查找位置
    var index = set.index(of: "XuBaoAiChiYu");
    print(index!)
    
    index = set.index(where: { (string) -> Bool in
        return string == "XuBaoAiChiYu"
    })
    print(index!)
    
    let string = set[index!]
    print(string)
    
    print("--------------")
    //無序遍歷
    for item in set {
        print(item)
    }
    print("--------------")
    //有序遍歷
    for item in set.sorted() {
        print(item)
    }
    print("--------------")
    
    /*  print
     
     ["1045214799", "iOS", "XuBaoAiChiYu"]
     contains:true
     SetIndex<String>(_value: Swift.SetIndexRepresentation<Swift.String>._native(Swift._NativeSetIndex<Swift.String>(nativeStorage: , offset: 3)))
     SetIndex<String>(_value: Swift.SetIndexRepresentation<Swift.String>._native(Swift._NativeSetIndex<Swift.String>(nativeStorage: , offset: 3)))
     XuBaoAiChiYu
     --------------
     1045214799
     iOS
     XuBaoAiChiYu
     --------------
     1045214799
     XuBaoAiChiYu
     iOS
     --------------
     
     */   
}
增加和刪除
//增加和刪除
fileprivate func testAddAndRemove() {
    
    var set:Set<String> = ["iOS","XuBaoAiChiYu","1045214799"]
    print(set)
    
    set.insert("Swift") //增加
    print(set)
    
    var str = set.remove("Swift") //刪除指定元素 並返回刪除的元素
    print(str!)
    
    str = set.remove(at: set.index(of: "XuBaoAiChiYu")!) //根據位置刪除
    print(str!)
    
    str = set.removeFirst() //刪除首個
    print(str!)
    
    set.removeAll() //刪除所有
    print(set)
    
    /*  print
     
     ["1045214799", "iOS", "XuBaoAiChiYu"]
     ["Swift", "iOS", "1045214799", "XuBaoAiChiYu"]
     Swift
     XuBaoAiChiYu
     iOS
     []
     
     */
}
結合和重組
//結合和重組
fileprivate func testCombiningAndRecombining() {
    
    let a:Set<String> = ["XuBaoAiChiYu", "1045214799", "swift"]
    print(a)
    let b:Set<String> = ["iOS", "1045214799", "swift"]
    print(b)
    let c:Set<String> = ["XuBaoAiChiYu", "1045214799", "swift", "iOS"]
    print(c)
    
    print("isEqual:\(a == b)") //是否相等
    print("isSubsetOf:\(a.isSubset(of: c))") //a是否是c的子集
    print("isSupersetOf:\(c.isSuperset(of: a))") //a是否是c的子集
    
    var set = a.intersection(b) //a交b 返回新set
    print(set)
    set = a.union(b)         //a並b
    print(set)
    set = a.subtracting(b)      //a差b 即a-a交b
    print(set)
    set = a.symmetricDifference(b)   //a並b-a交b
    print(set)
    
    // 不返回結果集,直接修改前set
    set.formIntersection(b)   //set交b,結果在set中
    print(set)
    set.formUnion(b)       //a並b
    print(set)
    set.subtract(b)    // a差b,即a-a交b
    print(set)
    set.formSymmetricDifference(b) //a並b-a交b
    print(set)
    
    /*  print
     
     ["swift", "1045214799", "XuBaoAiChiYu"]
     ["swift", "iOS", "1045214799"]
     ["iOS", "swift", "1045214799", "XuBaoAiChiYu"]
     isEqual:false
     isSubsetOf:true
     isSupersetOf:true
     ["swift", "1045214799"]
     ["iOS", "swift", "1045214799", "XuBaoAiChiYu"]
     ["XuBaoAiChiYu"]
     ["iOS", "XuBaoAiChiYu"]
     ["iOS"]
     ["swift", "iOS", "1045214799"]
     []
     ["swift", "iOS", "1045214799"]
     
     */
}
排序
//排序
fileprivate func testSort() {
    let set:Set<String> = ["XuBaoAiChiYu", "1045214799", "swift", "iOS", "1045214799",]
    print(set)
    
    var array = set.sorted() //排序 升序
    print(array)
    
    array = set.sorted { (str1, str2) -> Bool in
        return str1 < str2
    }
    print(array)
    
    //簡寫
    array = set.sorted(by: { str1,str2 in str1 < str2 })
    print(array)
    
    array = set.sorted(by: {$0 > $1})
    print(array)
    
    array = set.sorted(by: >)
    print(array)
    
    /*  print
     
     ["iOS", "swift", "1045214799", "XuBaoAiChiYu"]
     ["1045214799", "XuBaoAiChiYu", "iOS", "swift"]
     ["1045214799", "XuBaoAiChiYu", "iOS", "swift"]
     ["1045214799", "XuBaoAiChiYu", "iOS", "swift"]
     ["swift", "iOS", "XuBaoAiChiYu", "1045214799"]
     ["swift", "iOS", "XuBaoAiChiYu", "1045214799"]
     
     */
}



通過本文的學習希望對您了解和學習ios開發的相關知識有一些好的幫助.感謝關注本站.我們將為您收集更多更好的ios開發教程.

【Swift_Set詳解】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved