https://itunes.apple.com/us/book/swift-programming-language/id881256329?mt=11
https://itunes.apple.com/us/course/developing-ios-8-apps-swift/id961180099
https://itunes.apple.com/cn/course/ios-development-in-swift/id950659946
What declared with var is considered mutable while let is immutable.
var myVariable = 42 myVariable = 50 let myConstant = 42 ____________________________ let implicitInteger = 70 let implicitDouble = 70.0 let explicitDouble: Double = 70 ____________________________ let label = "The width is" let width = 94 let widthLabel = label + String(width) ____________________________ let apples = 3 let oranges = 5 let appleSummary = "I have \(apples) apples." //\() is a way to include values in strings let fruitSummary = "I have \(apples+oranges) pieces of fruit."________________________
Create arrays and dictionaries using brackets([]), and access their elements by writing the index or key in brackets.
var shoppingList = ["apple", "banana", "water"] shoppingList[1] = "a piece of banana" var occupations = [ "Jason": "Captain", "Kaylee": "Mechanic", ] occupations["Jayne"] = "Public Relations" ________________________ let emptyArray= [String]() let emptyDictionary= [String: Flocat]() ________________________ shopplingList= [] occupations= [:] ________________________ import Foundation var arrayAny: [AnyObject] = [] arrayAny += [1, "Two", false] arrayAny.append(4.0) ________________________
Conditionals: if , switch
loops:for-in, for, while
let individualScore = [75, 43, 103, 87, 12] var teamScore = 0 for score in individualScore { if(score > 50) { teamScore += 3 } else { teamScore += 1 } } println(teamScore) teamScore //This is a simple way to see the value of a variable inside a playgroud. _____________________________ var optionalString: String? = "Hello" optionalString ==nil var optionalName: String? = "Jason Wang" var greeting = "Hello!" if let name = optionalName { greeting = "Hello, \(name)" } _____________________________ var results: [int] = [] //same as var result: Array[Int] for i: Int in 0..<100 { if i % 7 == 0 { results.append(i) } } results _____________________________ func color(thing: String?) -> String? { if let someThing = thing { switch someThing { case let x where x.hasPrefix("green"): return "green" case "apple": return "red" case "pineapple","banana": return "yellow" default: return "unknow" } } else { return nil } } color("apple") color("green apple") color("banana") color("someThing") color(nil)
func location(city: String) -> (Double, Double) { var list = [ "Shanghai" : (12.233, 15.332), "Beijing" : (33.222, 31.333) ] return list[city] ?? (0, 0) } location("Shanghai") location("Wuhan")
var odds = [1, 3, 5, 7].map({number in 3*number}) odds import Foundation var array: [UInt32] = [] for _ in 0..<10 { array.append(arc4random_uniform(UInt32(100))) } array sort(&array){ $0 < $1 } array
原文地址:http://blog.csdn.net/jason_wang1989/article/details/43482701