Swift - 复杂数据类型说明(数组,字典,结构体,枚举)

Swift - 复杂数据类型说明(数组,字典,结构体,枚举),第1张

概述1,数组 - Array 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 var  types = [ "none" , "warning" , "error" ]   //省略类型的数组声明           var  menbers = [ String ]()  / 1,数组 - Array
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 var types = [ "none" , "warning" "error" ] //省略类型的数组声明 menbers = [ String ]() //声明一个空数组 menbers.append( "six" ) //添加元素 menbers += [ "seven" //添加元素 menbers.insert( "one" //指定位置添加元素 menbers[0] = "message" //通过下标修改数组中的数据 menbers[0...2] = [ "message" "hangge" "com" //通过小标区间替换数据(前3个数据) menbers.count //获取数组元素个数 menbers.isEmpty //判断数组是否为空 menbers.removeAtIndex(2) //删除下标为2的数组 menbers.removeLast() //删除最后一个元素 menbers.removeAll(keepCapacity: true //删除数组中所有元素 let addStringArr = types + menbers //数组组合 //使用for in 实现数组遍历 for value in menbers{ print ( "\(value)" ); } //通过enumerate函数同时遍历数组的所有索引与数据 (index,value) menbers. enumerate (){ "索引:\(index) 数据:\(value)" ); } //交换元素位置(第2个和第3个元素位置进行交换) swap(&menbers[1],&menbers[2])

2,字典 - Dictionary(即键值对) 25
empty = [ String : Int //建立个空字典 myDic = [ "name" : "url" "hangge.com" //声明一个字典 myDic[ "address" ] = "china" //添加或修改key值 myDic.removeValueForKey( //删除"name"这个key值 ] = nil //同样可以删除"name"这个key值 myDic.keys //访问字典的key集合 myDic.values //访问字典的values集合 //遍历字典 (key,monospace!important; min-height:auto!important; background:none!important">myDic { "\(key):\(value)" ); } //只遍历字典的键(key) key myDic.keys { "\(key)" ); } //只遍历字典的值(value) myDic.values { ); }

3,结构体 - struct @H_192_403@ 10
//创建一个结构体 struct BookInfo { var ID = 0 name : = "Defaut" Author "Defaut" } book1: BookInfo //默认构造器创建结构体实例 book2 = ( :0021, "航歌" //调用逐一构造器创建实例 book2. = 1234 //修改内部值

4,枚举 - enum 30
enum Compasspoint { case north South East West directionTohead = . West Planet { Mercury = 1 Venus = 2 Earth = 3 } earthsOrder = .rawValue //rawValue来获取他的原始值:3 possiblePlanet = (rawValue: 2) //通过原始值来寻找所对应的枚举成员:Venus Direction { Up Down func description() -> { switch ( self ){ case Up : return "向上" Down : "向下" } } .description())
总结

以上是内存溢出为你收集整理的Swift - 复杂数据类型说明(数组,字典,结构体,枚举)全部内容,希望文章能够帮你解决Swift - 复杂数据类型说明(数组,字典,结构体,枚举)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1062128.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存