Swift 1、相比Objective-C的一些变化

129 阅读1分钟

一、字符串用法
二、数组字典的用法
三、其他不同

一、字符串用法

  1. 数字转字符串的用法
  • 通过String()转化为字符串并以+拼接
    let num = 88
    let str = "this is a num "
    let result = str + String(num)
    
  • 通过\()的方式来转化为字符串
    let num = 88
    let result = "this is a num \(num)"
    
  1. 使用三个双引号"""来为字符串表示输入多行内容
    let num = 88
    let result = """
    this is multiple line
    "中间可以随意加引号 \(num)"
    感觉功能类似MarkDown的代码高亮```
    """
    

二、数组字典的用法

  1. 数组字典可变和不可变是根据letvar控制
    var emptyArray = [String]()
    emptyArray.append("1")
    
    let dic = [String : String]()
    //error dicM1["1"] = "2"
    
  2. Swift面向协议的思想,数组和字典内存储的内容不再局限于对象了
    let array = [1, 2, 3]
    
    var dicM = [Int:Int]()
    dicM[1] = 1000
    

三、其他不同

  1. 自己创建的文件不再需要通过Import导入 系统自动已经帮你导入了,你只需要保证不会重名