刚刚接触swift,不怎么喜欢看英文文档,在网上找了半天中文文档一无所获,还是自己简单的翻译一下吧,在这里做个记录,同时也帮助下和我一样不爱看英文的swift小白朋友
Simple Values
使用var和let声明一个常数和变量。常数的值在编译时需分配一个值。可很多地方调用。
var myVariable = 10
myVariable = 20 //此时编译器或自动推断myVariable为整数
let myConstant = 30
如果没有初始值,或初始值没有指定类型,可以通过变量的赋值来指定该类型
let implicitInteger = 70
let implicitDouble = 70.0
let explicitDouble: Double = 70
拼接与转换
let label = "The width is "
let width = 94
let widthLabel = label + String(width)
输出:
"The width is 94"
有一种更简单的方法包括在字符串值:写在括号中的值,并写上一个反斜杠(\)在括号。例如:
let apples = 3
let oranges = 5
let appleSummary = "I have \(apples) apples."
let fruitSummary = "I have \(apples + oranges) pieces of fruit."
输出
"I have 8 pieces of fruit."
let age = 12
let person_age = "我已经 \(age)岁了"
数组
使用括号([ ])创建数组和字典,并通过在括号中的索引或键访问它们的元素。
var shopping = ["catfish","water","tulips"]
shopping[1] = "bottle water" //替换数组中索引为1元素的值
输出
["catfish", "bottle water", "tulips"]
空数组(初始化语法)
let emptyArray = [String]()
or
shopping = []
字典
var occupations = ["www":"qqqq","eeeee":"www"]
occupations["aaaa"] = "dddd"//向字典中追加数据
输出:
["www": "qqqq", "eeeee": "www", "aaaa": "dddd"]
空字典(初始化语法)
let emptyDictionary = [String: Float]()
or
occupations = [:]
持续更新。。