0、swift语法在语句结束时可以不用分号结束
1、swift中打印log语法不再用NSLog,还原为print
print("Hello, world!")
2、属性声明,swift用let来声明常量,var来声明变量。因为swift编译器在编译时可以推断变量/常量的类型,所以你不用明确的声明其类型。
var myVariable = 42
myVariable = 50
let myConstant = 42
如果初始值不能代表所需要的类型,需要在后面声明类型,用冒号分割。
let implicitInteger = 70
let implicitDouble = 70.0
let explicitDouble: Double = 70
3、如果要把类型转换为其他类型,需要显示转换
let label = "The width is"
let width = 94
let widthLabel = label + String(width)
4、把值转换成字符串的形式也可以用()的形式,()内放变量
let apples = 3
let oranges = 5
let appleSummary = "I have \(apples) apples."
let fruitSummary = "I have \(apples + oranges) pieces of fruit."
5、需要写多行的字符串内容时,可以使用三个引号(""")来包含。
let quotation = """
I said "I have (apples) apples."
And then I said "I have (apples + oranges) pieces of fruit."
"""
6、数组和字典都可以使用方括号 [] 来进行创建,字典的最后一个元素后面可以有一个逗号。素组使用下标访问元素,字典使用key值访问元素
// 数组
var shoppingList = ["catfish", "water", "tulips", "blue paint"]
shoppingList[1] = "bottle of water"
// 字典
var occupations = [ "Malcolm": "Captain", "Kaylee": "Mechanic",]
occupations["Jayne"] = "Public Relations"