Swift Tour 随笔总结 (1)_移动开发_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > 移动开发 > Swift Tour 随笔总结 (1)

Swift Tour 随笔总结 (1)

 2015/1/3 23:03:18  极地渔翁.NTLD  程序员俱乐部  我要评论(0)
  • 摘要:letConstantvarVariableletimplicitInteger=70letimplicitDouble=70.0letexplicitDouble:Double=70Theso-calledtypeimplicationsToincludevalueinstrings:letfruitSummary="Ihave\(apples+oranges)piecesoffruit."ArraysandDicts:varshoppingList=["catfish","water"
  • 标签:总结 随笔

let Constant

var Variable


let implicitInteger = 70
let implicitDouble = 70.0
let explicitDouble: Double = 70

 

The so-called type implications

 

To include value in strings:

let fruitSummary = "I have \(apples + oranges) pieces of fruit."
 

Arrays and Dicts:

var shoppingList = ["catfish", "water", "tulips"]
shoppingList[1] = "bottle of water"
var occupations = [
    "Malcolm": "Captain",
    "Kaylee": "Mechanic",
]
occupations["Jayne"] = "Public Relations"
//empty
let emptyArray = String[]()
let emptyDictionary = Dictionary()
 

Control Flow and Optional Binding

For each


let individualScores = [75, 43 103, 87, 12]
var teamScore = 0
for score in individualScores {
    if score > 50 {
        teamScore += 3
    } else {
        teamScore += 1
    }
}
teamScore

 

Optional Binding


var optionalString: String? = "Hello"
optionalString == nil

var optionalName:String? = "John Appleseed"
var greeting = "Hello!"

//optional binding
if let name = optionalName {
    greeting = "Hello, \(name)"
}

If the optional value is nil, the conditional is false and the code in braces is skipped.

Otherwise, the optional value is unwrapped and assigned to the constant after let, which makes the unwrapped value available inside the block of code.

发表评论
用户名: 匿名