Swift 6: 數組與字典(OC與swift混編)

1----數組----

1:數組簡介

(1):OC中的數組能夠存放任意類型的數據AnyObject

(2):數組在內存中存放是有序的,所以對一個集合中存放的數據進行排序使用的就是數組

(3):不可變NSArray繼承與可變數組NSMutableArray

2:不可變數組方法

//1.數組的創(chuàng)建

let arr1: NSArray = NSArray.init(objects: 10,"hello",1.234)
print(arr1)

let arr2: NSArray = [1,2,3,4,5,7,8]
print(arr2)

//將數組所有內容全部賦給另一數組 (表格、集合視圖中)
let arr3 = NSArray.init(array: arr2)
print(arr3)

let arr4 = NSArray.init(objects: arr3,100,"HELLO")
print(arr4)

print(arr4.count)

//2.遍歷數組

for i in 0..<arr4.count {
    print(arr4[i])
}
for i in arr4 {
    print(i)
}

//3.通過元素內容 獲取對應的索引

if arr4.containsObject(100) {
    print(arr4.indexOfObject(100))
}

//4.獲取數組中第一個元素和最后一個元素

print(arr4.firstObject)
print(arr4.lastObject)

//5.將數組中的元素用指定的字符串進行拼接

//拼接的結果是一個字符串
let arr_1: NSArray = ["中國","????","China",75]
let str_1 = arr_1.componentsJoinedByString("-")
print(str_1)

//6.將字符串按照指定的字符串進行分割 --- 分割的結果是一個數組

var str_path: NSString = "http://www.baidu.com:1234/s?wd=ios&rsv_sug4=82&inputT=802"
var array: NSArray = str_path.componentsSeparatedByString("http://")
let lastStr: NSString = array.lastObject as! NSString
var lastArr: NSArray = lastStr.componentsSeparatedByString(":")
print(lastArr.firstObject!)

//7.isKindOfClass判斷某個元素是否屬于某個類

class Cat {
    func wash() -> Void {
        print("喵星人正在洗臉")
    }
}
let tom = Cat.init()
var array_2: NSArray = [1,"hello",tom]
for i in array_2 {

    
    if i.isKindOfClass(Cat) {
        (i as! Cat).wash()
    }
}

3.可變數組方法

//1.定義可變數組對象

var mutableArr1 = NSMutableArray.init(capacity: 0)
var mutableArr2 = NSMutableArray.init(array: [1,2,3,4,5,6,78])
var mutableArr3: NSMutableArray = [1,"hello","1611"]

//2.添加

//追加
mutableArr1.addObject("123")
mutableArr1.addObject([1,2,3,4,5,6,7,8,9])
mutableArr1.addObject("123")
print(mutableArr1)
//插入
mutableArr1.insertObject("QianFeng", atIndex: 0)
print(mutableArr1)

//3.刪除

mutableArr1.removeLastObject()

//刪除指定元素 如果存在多個就一起刪除
if mutableArr1.containsObject("123") {
    
    mutableArr1.removeObject("123")
    print(mutableArr1)
}

//刪除指定索引處的元素
//mutableArr1.removeObjectAtIndex(0)

//刪除一定范圍內的元素
mutableArr1.removeObjectsInRange(NSRange.init(location: 0, length: 1))

mutableArr1.removeAllObjects()

//4.修改

mutableArr2.replaceObjectAtIndex(0, withObject: ["one","two","three"])
print(mutableArr2)

//5.交換數組中元素的顯示位置

mutableArr2.exchangeObjectAtIndex(0, withObjectAtIndex: 2)
print(mutableArr2)

2----字典------

1.字典簡介

(1):OC中字典也是由多個鍵值對組成

(2):字典用于查找,key值是唯一的,不能重復

(3):字典在內存中存放是無序的

(4):OC中字典分為兩種:可變字典NSMutableDictionary和不可變字典NSDictionary.可變字典是不可變字典的子類 所以可變字典能使用不可變字典的所有方法

(5):OC字典能存放任意類型的數據 通常鍵是字符串 值是任意類型

2.不可變字典方法

//1.創(chuàng)建字典

var dic1: NSDictionary = ["1":"one","2":"two","3":"three"]

var dic2 = NSDictionary.init(objects: [1,2,3,3], forKeys: ["one","two","three","four"])

var dic3 = NSDictionary.init(dictionary: dic1)

//2.字典的遍歷

for (key,_) in dic1 {

    let keystr = key as! String
    print(keystr,dic1[keystr]!)
}

//3.獲取字典中鍵值對個數

print(dic1.count)

//4.獲取字典的所有鍵和所有值

print(dic1.allKeys)
print(dic1.allValues)

//5.通過值獲取所有的鍵

print(dic2.allKeysForObject(3))

3:可變字典方法

let mutableDic1 = NSMutableDictionary.init(capacity: 0)
let mutableDic2 = NSMutableDictionary.init(dictionary: dic1)

//1.添加

mutableDic1.setObject("盜墓筆記", forKey: "movie")
mutableDic1.setObject("喬布斯傳", forKey: "book")

//字典或者數組中存在中文直接調用字典或者數組對象 中文是要以UTF8格式轉碼
func printDic(mutableDic1: NSMutableDictionary) {
    
    for (key,_) in mutableDic1 {
        let keystr = key as! String
        print(keystr,mutableDic1[keystr]!)
    }
}

//2.修改

mutableDic1.setObject("美人魚", forKey: "movie")
printDic(mutableDic1)

//3.刪除

mutableDic1.removeObjectForKey("movie")
printDic(mutableDic1)

mutableDic1.removeAllObjects()
printDic(mutableDic1)
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容