美文网首页
swift (容器)

swift (容器)

作者: cyhai | 来源:发表于2019-08-28 09:14 被阅读0次
Array
        let Arr:Array = ["数组元素1","数组元素2","数组元素3"]
NSMutableArray
        let muArr = NSMutableArray()

添加元素

 for arrStr:String in Arr {
            //add(元素)
            muArr.add(arrStr)
        }

移除元素

//TODO:移除
        muArr.removeObject(at: 1)
        print("数组移除相应下标元素\n",muArr)
        muArr.removeAllObjects()

元素拼接

//TODO:拼接
        let string_com:String = muArr.componentsJoined(by: ",")

字符串分离抽取

//TODO:分离
        muArr.addObjects(from: string_com.components(separatedBy: ","))

数组遍历

//TODO:结合enumerate()遍历数组
        for (index,item) in muArr.enumerated() {
            
            print("enumerate遍历数组:下标:\(index)  元素:\(item)")
            
        }
Dictionary
let diction:Dictionary = ["key1":"values1","key2":"values2","key3":"values3"]
NSMutableDictionary
 let muDition = NSMutableDictionary()

添加键值对

for key:String in diction.keys {
            muDition.setValue(diction[key], forKey: key)
        }

指定类型字典

//TODO:一个特定类型字典
        var dictionIS :[Int:String] = [1:"固定类型1",2:"固定类型2",3:"固定类型3"]

遍历字典

//TODO:遍历字典
        for (key,values) in dictionIS {
            print("key=\(key)  values\(values)")
        }
Set
func setClass() -> Void {
        var set = Set<String>()
//插入数据
        set.insert("001")
        set.insert("002")
        print(set)
//移除
        set.remove("001")
        print(set)
    }

相关文章

网友评论

      本文标题:swift (容器)

      本文链接:https://www.haomeiwen.com/subject/etxyectx.html