美文网首页
swift 基本认识

swift 基本认识

作者: 橙色酱油cc | 来源:发表于2016-07-21 23:17 被阅读0次

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 基本认识

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