你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> Swift_數組詳解

Swift_數組詳解

編輯:IOS開發綜合

本文將為大家說明Swift_數組詳解的相關介紹,具體實例請看下文

Swift_數組詳解 初始化
//初始化
fileprivate func testInit() {
    //空數組
    var  array = [Int]()
    print(array)
    array = []
    print(array)
    array = Array<Int>()
    print(array)
    
    //有數值的數組
    array = [Int](repeating: 0, count: 3)
    print(array)
    array = [0, 0, 0]
    print(array)
    
    /* print
     
     []
     []
     []
     [0, 0, 0]
     [0, 0, 0]
     
     */
}
查詢相關信息
//查詢相關信息
fileprivate func testQuerying() {
    var array = ["XuBaoAiChiYu", "QQ", "1045214799"]
    print("contains:\(array.contains("XuBaoAiChiYu"))") //是否存在這個元素
    print("count:\(array.count)")       //有多少個元素
    print("capacity:\(array.capacity)") //有多少個元素
    print("startIndex:\(array.startIndex)") //首坐標
    print("endIndex:\(array.endIndex)")     //尾腳標
    print("first:\(array.first)") //首元素
    print("last:\(array.last)")   //尾元素
    print("isEmpty:\(array.isEmpty)") //是否為空
    
    //獲取指定位置的元素
    var temp = (array as NSArray).object(at: 0)
    temp = array[0]
    print("\(temp)")
    
    //獲取多個元素
    let nRange : NSRange = NSMakeRange(0, 3)
    let nIndexSet = IndexSet(integersIn: nRange.toRange() ?? 0..<0)
    let ocArray = (array as NSArray).objects(at: nIndexSet)
    print("\(ocArray)")
    
    let range = (0 ..< 3)
    let sArray = array[range]
    print("\(sArray)")
    
    //遍歷
    //只獲取元素
    for item in array {
        print(item)
    }
    //遍歷元素和所處的位置
    for (index, value) in array.enumerated() {
        print("\(index):\(value)")
    }
    
    /*  print
     
     contains:true
     count:3
     capacity:3
     startIndex:0
     endIndex:3
     first:Optional("XuBaoAiChiYu")
     last:Optional("1045214799")
     isEmpty:false
     XuBaoAiChiYu
     [XuBaoAiChiYu, QQ, 1045214799]
     ["XuBaoAiChiYu", "QQ", "1045214799"]
     XuBaoAiChiYu
     QQ
     1045214799
     0:XuBaoAiChiYu
     1:QQ
     2:1045214799
     
     */
}
查找元素位置
//查找元素位置
fileprivate func testFindingObjects() {
    
    let array = ["XuBaoAiChiYu", "QQ", "1045214799"]
    
    // 查找元素
    var index = array.index(of: "QQ")
    print(index!)
    
    // closures查找
    index = array.index { (str:String) -> Bool in
        if str == "QQ" {
            return true
        }
        return false
    }
    print(index!)
    
    // 簡寫
    index = array.index{ str in return str == "QQ" }
    print("index:\(index)")
    index = array.index{ str in str == "QQ" }
    print("index:\(index)")
    index = array.index{ $0 == "QQ"}
    print("index:\(index)")
    
    /* print
     
     1
     1
     index:Optional(1)
     index:Optional(1)
     index:Optional(1)
     
     */
}
增加元素
//增加元素
fileprivate func testAdding() {
    
    var array = [String]()
    print(array)
    
    //增加一個
    array.append("XuBaoAiChiYu")
    print(array)
    array += ["XuBaoAiChiYu"]
    print(array)
    
    //增加多個
    array.append(contentsOf: ["QQ", "1045214799"])
    print(array)
    array += ["QQ", "1045214799"]
    print(array)
    
    //插入
    array.insert("1045214799", at: 0)
    print(array)
    //插入多個
    array.insert(contentsOf: ["1045214799", "QQ"], at: 0)
    print(array)
    
    /* print
     
     []
     ["XuBaoAiChiYu"]
     ["XuBaoAiChiYu", "XuBaoAiChiYu"]
     ["XuBaoAiChiYu", "XuBaoAiChiYu", "QQ", "1045214799"]
     ["XuBaoAiChiYu", "XuBaoAiChiYu", "QQ", "1045214799", "QQ", "1045214799"]
     ["1045214799", "XuBaoAiChiYu", "XuBaoAiChiYu", "QQ", "1045214799", "QQ", "1045214799"]
     ["1045214799", "QQ", "1045214799", "XuBaoAiChiYu", "XuBaoAiChiYu", "QQ", "1045214799", "QQ", "1045214799"]
     
     */
}
刪除元素
//刪除元素
fileprivate func testRemoving() {
    var array = ["XuBaoAiChiYu", "QQ", "1045214799"]
    print(array)
    
    array = array + array + array + array
    print(array)
    
    var temp = array.removeFirst() //刪除首個元素 並返回刪除的元素
    print(array)
    print(temp)
    
    array.removeFirst(2) //刪除前兩個元素
    print(array)
    
    temp = array.removeLast() //刪除最後一個元素
    print(array)
    
    temp = array.remove(at: 0) //刪除指定位置的元素
    print(array)
    
    array.removeSubrange((array.indices.suffix(from: 0))) //刪除指定范圍的元素
    print(array)
    
    array.removeAll() //刪除所有元素
    print(array)
    
    /* print
     
     ["XuBaoAiChiYu", "QQ", "1045214799"]
     ["XuBaoAiChiYu", "QQ", "1045214799", "XuBaoAiChiYu", "QQ", "1045214799", "XuBaoAiChiYu", "QQ", "1045214799", "XuBaoAiChiYu", "QQ", "1045214799"]
     ["QQ", "1045214799", "XuBaoAiChiYu", "QQ", "1045214799", "XuBaoAiChiYu", "QQ", "1045214799", "XuBaoAiChiYu", "QQ", "1045214799"]
     XuBaoAiChiYu
     ["XuBaoAiChiYu", "QQ", "1045214799", "XuBaoAiChiYu", "QQ", "1045214799", "XuBaoAiChiYu", "QQ", "1045214799"]
     ["XuBaoAiChiYu", "QQ", "1045214799", "XuBaoAiChiYu", "QQ", "1045214799", "XuBaoAiChiYu", "QQ"]
     ["QQ", "1045214799", "XuBaoAiChiYu", "QQ", "1045214799", "XuBaoAiChiYu", "QQ"]
     []
     []
     
     */
}
替換元素
//替換元素
fileprivate func testReplacing() {
    
    var array = ["XuBaoAiChiYu", "QQ", "1045214799"]
    print(array)
    
    array[1] = "IOS" // 替換指定位置的元素
    print(array)
    
    // 替換指定范圍的元素
    array[0..<3] = ["a", "b", "c"]
    print(array)
    
    let subRange: CountableRange<Int> = (array.indices.suffix(from: 1))
    array.replaceSubrange(subRange, with: ["c","d","e"])
    print(array)
    
    /*  print
     
     ["XuBaoAiChiYu", "QQ", "1045214799"]
     ["XuBaoAiChiYu", "IOS", "1045214799"]
     ["a", "b", "c"]
     ["a", "c", "d", "e"]
     
     */
    
}
排序
//排序
fileprivate func testSorting() {
    var array = ["XuBaoAiChiYu", "QQ", "1045214799"]
    print(array)
    
    array = array.sorted()
    print(array)
    
    //自定義排序
    func backwards(_ s1: String, _ s2: String) -> Bool {
        return s1 > s2
    }
    array = array.sorted(by: backwards)
    print(array)
    
    //簡寫
    array = array.sorted(by: { (s1: String, s2: String) -> Bool in
        return s1 > s2
    })
    print(array)
    
    array = array.sorted(by: <)
    print(array)
    
    /*  print
     
     ["XuBaoAiChiYu", "QQ", "1045214799"]
     ["1045214799", "QQ", "XuBaoAiChiYu"]
     ["XuBaoAiChiYu", "QQ", "1045214799"]
     ["XuBaoAiChiYu", "QQ", "1045214799"]
     ["1045214799", "QQ", "XuBaoAiChiYu"]
     
     */
}
文件讀和寫
//文件讀和寫
fileprivate func testReadingAndWriting() {
    
    //Document目錄
    let documents:[String] = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
    let docDirPath = documents.first!
    let path = (docDirPath as NSString).appendingPathComponent("test.plist")
    let url = URL(fileURLWithPath: path)
    
    var array = ["XuBaoAiChiYu", "QQ", "1045214799"]
    
    //寫
    (array as NSArray).write(toFile: path, atomically: true)
    (array as NSArray).write(to: url, atomically: true)
    
    //讀
    array = NSArray(contentsOfFile: path) as! Array
    print(array)
    
    array = NSArray(contentsOf: url) as! Array
    print(array)
    
    /*  print
     
     ["XuBaoAiChiYu", "QQ", "1045214799"]
     ["XuBaoAiChiYu", "QQ", "1045214799"]
     
     */
}



多多關注本站,我們將為您收集更多的Android開發相關文章.

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

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