在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
在OC中数组,字典,集合有自己的表示方法,分别是Array,Dictionary,Set 与 String 都属于数值类型变量,他们都属于结构体。 使用简介灵活多变,个人感觉可读性变差了很多,用起来由点别扭,还需要慢慢适应。 基本上功能都是照办OC的,主要包括创建,增,删,改,查,遍历。下面从数组开始说起。 1. Array ,数组,线性机构,按顺序存储结构,有对应的下表标 创建方式贼多,大家根据自己的喜好随便选一种吧, var firstArray:Array<Int> = Array<Int>() var secondArray:Array = Array<Int>() var thirdArray = Array<Int>() var fourthArray:Array<Int> = Array() var fivethArray:Array<Int> = [Int]() var sixthArray:[Int] = [Int]() var seventhArray:[Int] = [] var eighthArray = [1,2,3,4,5,5,6] var ninethArray:[Int] //最后一种只生成没有初试化 2. 增加数据 append() + = firstArray.append(250) OC: addObject: firstArray[0] = "500" firstArray += ["180","360","720","a","g"] //添加另外一个数组 firstArray.count firstArray.isEmpty 3. 插入元素 inset() firstArray.insert("iphone6 plus",at Index:1) 其它元素需要后移动,比较消耗性能 4. 删除元素 removeAtIndex(1) 它的返回值为移除的数组元素类型 var res: String = firstArray.removeAtIndex(1) res = firstArray.removeFirst() res = firstArray.removeLast() var range = Range(start:0, end:2) firstArray.removeRange(Range) 5. 修改元素 通过下标直接修改 shoppingList[1] = "price" shoppingList.removeAll() 6. 从数组中获取部分元素同时声称一个数组 var subList = shoppingList[0..<3] /**遍历*/ for elements in subList { print(elements) }
for var i = 0; i < subList.count;i++ { print("第\(i)个元素是\(subList[i])") } for (index,value) in subList.enumerate() { print("第\(index) \(value)") } subList subList[0] 2. 字典部分,创建方式 var dictionary1:Dictionary<String,Int> = Dictionary<String,Int>() var dictionary2 = Dictionary <String,Int>() var dictionary3:Dictionary = Dictionary<String,Int>() var dictionary4:Dictionary<String,Int> = Dictionary() var dictionary5:Dictionary<String,Int> = [String:Int]() var dictionary6:Dictionary<String,Int> = ["age":23,"age2":18] var dicitonary7 = ["age":18,"age2":33] //不能直接使用 nil 作为变量,但是可以使用可选值座位值 var dictionary8:[String:Int?] = ["age":23,"age":nil] //未初始化,只申明 var dictionary9:[String:Int] 3. 基本操作方法:
/**操作字典的方法*/ var airports:[String:String] = ["PEK":"Beijing airport","CAN":"GuangzhouBaiYun airport","SHA":"ShangHaiHongQiao airport"] airports.count airports["SZA"] = "Baoan airport" //如果eky不存在就是添加,如果存在就是修改 airports.count 4 . updataValue( value:xxx ,forkey:xxx) ,注意Swift key必须写成String类型 airports.updateValue("ShangHaiPuDong", forKey: "PVG") airports.count airports.isEmpty airports["TRA"] = "DaNei airport"
/**删除机场*/ airports.count airports["SZA"] = nil airports.count
5 .删除字典元素,注意不管是删除,还是添加过程中,都可以返回一个可选值Value removeValueForKey removeAll if let airport = airports.removeValueForKey("TRA"){ print("airport: \(airport)delete successfully") }else { print("meiyou duiying jichang") }
6. 获取字典中所有的keys 和所有的Values,使用数组类型进行强转 let airportCodes = [String] (airports.keys) let airportValues:[String] = [String] (airports.values)
//集合部分 Set,它具有无序性和唯一性,个人感觉基本没怎么用,除了网络并发请求外 1. Standard style var letters:Set<Character> = Set<Character>() 其余写法大家自己琢磨去吧,之要不报错基本都行,写法太多了,和字典数组都差不多。 其实只要保证左右两边结合起来能确定 它自身的类型(Set,Dictionary,Array),元素类型(Int,String,Character),然后右边必须有一个() 或者[] 就能满足要求了 2. 增,删,交,并,差,异或 var musics:Set<String> = ["Rock","Classical","Jazz"] music.isEmpty music.count music.insert("Jazz") if let removeMusic = musics.remove("Jazz") { print ("\(removeMusic) delete successfully") } else { print("element is not exist") } musics.contains("rock") for music in musics {print(music)} //排序 for music in music.sort() { print(music) } let oddDigits:Set = [1,3,4,5,6] let evemDigits:Set = [0,1,3,4,5] //union 并集 var newNumbers = oddDigits.union(evenDigits) print(newNumbers) //intersect 交集 newNumbers = oddDigits.intersect(evenDigits) //subtract 差集 newNumbers = oddDigits.subtract(evenDigits) //异或集合,把不相同的部分提取出来 newNumbers = oddDigits.exclusiveOr(evenDigits) 记住好四个单词, uinion,intersect,subract,exclusiveOr 就好了
|
请发表评论