//: Playground - noun: a place where people can play
import UIKit
var str = "Hello,playground"
var myVar = 45
myVar = 90
let myCon = 90
let implictInteger = 70
let implicitDouble = 80.0
let explicitDouble: Double = 700
let expFloat: Float = 4
//值永远不会隐式转换为另一种类型。如果你需要把一个值转换到不同类型,需要明确的构造一个所需类型的实例。
let label = "The width is "
let width = 100
let widthLabel = label + String(width)
//实现字符串中包含值:以小括号来写值,并用反斜线(\)放在小括号之前
let apples = 3
let oranges = 5
let appleSummary = "I have \(apples) apples."
let fruitSummary = "I have \(apples + oranges) pieces"
let impdouble = 90.0
let tom = "name"
let labe = "go to \(impdouble) \(tom) fname"
//创建一个数组和字典使用方括号"[]",访问其元素则是通过方括号中得索引或键。
var shoppingList = ["catish","water","tulips","blue paint"]
shoppingList[1] = "bottle of water"
var occupations = [
"Malcolm": "Captain",
"Kaylee": "Mechanic",
]
occupations["Jayne"] = "Public Re"
//创建一个空得数组或字典,使用初始化语法:
let emptyArray = [String]()
let emptyDictionary = Dictionary<String,Float>()
//如果类型信息无法推断,你可以写空的数组"[]" 和空的字典"[:]",例如你设置一个知道变量并传入参数到函数:
shoppingList = []
occupations = [:]
@H_407_301@ //数字字面量(Literals)var decimalInteger = 17//十进制
var binarryInteger = 0b10001//二进制
let octalInteger = 0o21//八进制
let hexadecimalInteger = 0x11//十六进制
//可选值绑定
var temp1: String? = "123"
if let temp2 = temp1?.toInt() {
println("\(temp1) has an \(temp2)")
}
原文链接:https://www.f2er.com/swift/326782.html