Swift 6: 数组与字典(OC与swift混编)

1----数组----

1:数组简介

(1):OC中的数组能够存放任意类型的数据AnyObject

(2):数组在内存中存放是有序的,所以对一个集合中存放的数据进行排序使用的就是数组

(3):不可变NSArray继承与可变数组NSMutableArray

2:不可变数组方法

//1.数组的创建

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..

//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("//")
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.创建字典

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)

你可能感兴趣的:(Swift 6: 数组与字典(OC与swift混编))