swift 基本认识

1,格式化创建字符串

let str1 = String(format: "%02d:%02d",1,2)

let str2 = String(format: "%02d-%02d", arguments:[1,2]);

print(str1)

print(str2)

2,数组 array(跟 OC 里面的数组是可以互相转换的)

let array1 = ["a","2","3"]

let array2 = [String]()//initWith/init 在 swift 里全部转化成()

//元素个数. count

//数组遍历 forin(明确指出数组类型)

for temp in array1 as [String]{

print(temp)

}

//元组遍历(涉及下标)

for (index, value) in array1.enumerate(){

print("index = \(index), value = \(value)")

}

//可变数组

var mutableArray = [String]()

mutableArray.append("hello")

mutableArray.append("world")

print(mutableArray)

//mutableArray.removeAll()

//mutableArray.removeFirst(n)//从第一个开始移除 n 个

//print(mutableArray)

3,字典 dictionary

let dict = ["key1":"value1","key2":"value2"]

print(dict)

//通过 key 值访问 value 值

print(dict["key1"])

//for in 遍历

for (tempkey, tempvalue) in dict{

print("key = \(tempkey), value = \(tempvalue)")

}

//可变字典

var dic2 = ["key":"value"]

//合并

for(tempkey, tempvalue) in dict{

//如果 key 存在,则是一个更新键值对的操作,如果不存在,就增加键值对

dic2[tempkey] = tempvalue

}

print(dic2)

//如果一个值可能为 nil, 那么这个值就是可选类型,用?标识

var a1:String?

//a1 = "12234"

//print(a1!)//Optional("12234") !:可选类型不能直接使用,必须强制解包

//!!!!对一个空的可选类型进行强制解包时会 crash

print(a1 ?? "234")//对可选类型进行判断,如果可选类型为 nil, 则给他一个默认值

4,if 没有非零即真的概念,只有 true false 的两种情况(swift 是强语言,不存在隐式转换)

let tempvalue = 10

if tempvalue > 5 {

print("tempvalue > 5")

}

5,可选类型的条件分支类型

let str:String? = "hello"

/**

*if-let是对可选类型的判断,如果可选类型为 nil, 则不执行代码块,如果不为空则用 tempStr来接收此刻这个可选类型的解包后的值

*/

if let tempStr = str{

print(tempStr)

}

/**

*if-let-where跟 if-let 相似, where 是对前面定义的这个局部变量再做一层判断

*/

if let tempStr = str where tempStr.characters.count > 2{

print(tempStr)

}

/**

*guard-let-else 如果可选类型为 nil, 则执行 code 代码块,最后一定要 return 如果不为 nil 则强制解包后的值赋值给 tempStr, 这样在{}外面就可以使用 tempStr

*/
//此操作需在函数中进行(return)
//let str:String? = "hello"

//guard let tempStr = str else{

//print("str为 nil")

//return不能少

//}

//

//print(tempStr)

6,switch 和 枚举

/**

*switch 不局限判断整型,可以使浮点型,也可以是字符串等等...

switch 判断后面的小括号可以省略,大括号不可以省

case 后面至少要有一条执行语句!!!并且 case 后面的大括号可以省, break 可以不写,不会造成贯穿,

default 一定要写,并且只能写在最后

*/



let f = 3.2

switch f{

case 3.0:

print("===3.0")

case 3.1:

print("===3.1")

case 3.2:

print("===3.2")

default:

print("unknow")

}



//该写法2.2后废弃

//for(var i = 0,i < 5,i++){

//

//}



for i in 0..<5{//0...5[0,5]

print("i = \(i)")

}

/**

*枚举(枚举值可以关联浮点,字符串,没有默认的关联值)

关联如果是 Int, 默认递增,如果不是 Int, 必须每个枚举值都要关联上对应的值

*/

enum Month:Int{

case January =10

case February

case March

case April

}

//如果明确指出一个变量/常量是属于哪种枚举类型的话,可以直接用. 枚举值赋值,否则就 枚举类型.枚举值

let month = Month.February

//let month:Month = .January

var month1 =Month.January

month1= .February

switch month{

case.January:

print("hashValue =\(month.hashValue),rawValue =\(month.rawValue)")

case.February:

print("hashValue =\(month.hashValue),rawValue =\(month.rawValue)")

case.March:

print("hashValue =\(month.hashValue),rawValue =\(month.rawValue)")

case.April:

print("hashValue =\(month.hashValue),rawValue =\(month.rawValue)")

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Swift 介绍 简介 Swift 语言由苹果公司在 2014 年推出,用来撰写 OS X 和 iOS 应用程序 ...
    大L君阅读 8,552评论 3 25
  • 53.计算字符 在字符串中获取字符值的数量, 可以使用字符串字符属性中的计数属性: let unusualMena...
    无沣阅读 4,852评论 0 4
  • 01-常量与变量 学习swift第一步打印Hello World print("Hello World") swi...
    iOS_恒仔阅读 10,652评论 2 19
  • YES 2014WWDC发布 常量和变量使用注意 在实际过程中,建议先定义常量,如果需要修改再改变为变量(更加安全...
    南冯阅读 3,641评论 0 0
  • 多少次轮回就有多少次伤悲! --题记 看着一片片飘落的金叶,再伟岸挺拔的古树也禁不住伤悲…… 不断地祈祷,不断地期...
    鹤洺阅读 1,196评论 6 6