Var 定义变量
Let 定义常量
printf() 打印
var num = 1 //int变量
var num = 10.0 //double 变量
字符串
NSString ---> String "String" 直接双引号 可以像脚本语言一样直接对字符串想加减,可以带入变量
let c = "cap"
"a"+"b"+c = "abcap"
let d = "I have \(c) apples"// \(c)将常量c填充入字符串,类似format、
数组
NSArray --->声明空数组的话使用[] 或者let emptyArray = [String]()
let arr = ["a","b","c"]
NSMutableArray--->
var arr = ["a","c"]
arr[0] = "apple" //替换元素
字典
NSDictionary--->声明空字典的话使用[:] 或者let emptyDictionary = [String: Float]()
let dic = ["keyA":"A","keyB":"b","keyC":"c"]
NSMutableDictionary--->
var dic = ["keyA":"A","keyC":"c"]
//addobject 添加
dic["KeyD"]="D" //相当于字典addobject
//setobject 设置
dic["D"] = "Dan"
print("dic=",dic)
//removeObjectForKey 移除某个key和对应的键值
dic["D"] = nil
//removeAllobject 移除所有
dic = [:]
print("dic)
原文链接:https://www.f2er.com/swift/325404.html