Swift Cheat Sheet [1] — Basic Types
常量和变量Varibalesvar myInt = 1 //inexplicit type var myExplicitInt : Int = 1 // explicit type var x = 1,y = 2,z = 3 //declare multiple integers myExplicitInt = 3 // set to another integer value Constantslet myInt = 1 myInt = 2 //compile-time error !!! 常量和变量的命名let π = 3.14159 let 你好 = "你好世界" let ???? = "dogcow" //可以用任何字符作为常量或变量名,包括Unicode字符 可选类型可选类型,暗示常量或者变量可以没有值。 let possibleNumber = "123" let convertedNumber = Int(possibleNumber) // convertedNumber 被推测为类型 "Int?", 或者类型 "optional Int" nil可以给可选变量赋值为nil来表示它没有值. var serverResponseCode: Int? = 404 // serverResponseCode 包含一个可选的 Int 值 404 serverResponseCode = nil // serverResponseCode 现在不包含值 如果你声明一个可选常量或者变量但是没有赋值,它们会自动被设置为nil var surveyAnswer: String? // surveyAnswer 被自动设置为 nil 注意:
if 语句以及可选值的强制解析(forced unwrapping)使用if语句和nil比较来判断一个可选值是否包含值 var convertedNumber : Int? = 10 if convertedNumber != nil{ print("convertedNumber has an integer value of (convertedNumber!)") } // 输出 "convertedNumber has an integer value of 10" 可选绑定(option binding)使用可选绑定(optional binding)来判断可选类型是否包含值,如果包含就把值赋给一个临时常量或者变量。可选绑定可以用在if和while语句中,这条语句不仅可以用来判断可选类型中是否有值,同时可以将可选类型中的值赋给一个常量或者变量 一个示例解析
包含多个可选绑定在条件判断语句中if let firstNumber = Int("4"),secondNumber = Int("42") where firstNumber < secondNumber { print("(firstNumber) < (secondNumber)") } // prints "4 < 42" 隐式解析可选类型(implicitly unwrapped optionals)在Swift构造的过程中,当可选类型第一次赋值之后,就可以确定之后一直有值。这种情况下,可选类型的可选状态被定义为隐式解析可选类型。把可选类型后边的问号改为叹号。 let possibleString: String? = "An optional string." let forcedString: String = possibleString! // 需要惊叹号来获取值 let assumedString: String! = "An implicitly unwrapped optional string." let implicitString: String = assumedString // 不需要感叹号 分号//Swift不强制要求在语句结尾处使用分号,当然,也可以按照自己的习惯添加 //当在同一行内写多条独立的语句时,必须要用分号! let cat = "??";print(cat) 整数整数范围使用min和max属性获取整数的最小值和最大值 let minValue = UInt8.min // minValue 为 0,是 UInt8 类型 let maxValue = UInt8.max // maxValue 为 255,是 UInt8 类型 Int
UInt
浮点数
String操作符+var myString = "a" let myImmutableString = "c" myString += "b" // ab myString = myString + myImmutableString //abc myImmutableString += "d" //compile-time error!!! 字符串插值(value)let count = 7 let message = "There are (count) days in a week" Bool值在if语句中的应用let turnipsAreDelicious = false if turnipsAreDelicious { print("Mmm,tasty turnips!") }else { print("Eww,turnips are horrible.") } 元组元组(tuples)把多个值组合成一个复合值。元组内的值可以是任意类型,并不要求是相同类型。 创建一个元组let http404Error = (404,"Not Found") //let http404Error = (404,"Not Found") 分解元组内容let http404Error = (404,"Not Found") let (statusCode,statusMessage) = http404Error print(("The status code is (statusCode)")) // 输出 "The status code is 404" print("The status message is (statusMessage)") // 输出 "The status message is Not Found" 用下划线_忽略一部分元组值如果你只需要一部分元组值,分解的时候可以把要忽略的部分用下划线(_)标记: let (justTheStatusCode,_) = http404Error print("The status code is (justTheStatusCode)") // 输出 "The status code is 404" 访问元组的单个元素print("The status code is (http404Error.0)") // 输出 "The status code is 404" print("The status message is (http404Error.1)") // 输出 "The status message is Not Found" 给元组的单个元素命名let http200Status = (statusCode: 200,description: "OK") 通过名字访问元组元素print("The status code is (http200Status.statusCode)") // 输出 "The status code is 200" print("The status description is (http200Status.description)") // 输出 "The status message is OK" 类型别名typealias AudioSample = UInt16 //使用typealias关键字来定义类型别名 var maxAmplitudeFound = AudioSample.min //maxAmplitudeFound 现在是 0 类型转换整数和浮点数整数 to 浮点数let three = 3 let pointOneFourOneFiveNine = 0.14159 let pi = Double(three) + pointOneFourOneFiveNine // pi 等于 3.14159,所以被推测为 Double 类型 浮点数 to 整数let integerPi = Int(pi) // integerPi 等于 3,所以被推测为 Int 类型 整数和字符串Int to Stringlet label = "The width is" let width = 94 let widthLabel = label + String(width)// The width is 94 String to Intcode1: var myString = "7" //7 var possibleInt = Int(myString) //7 print(possibleInt) //"Optional(7)n" code2: var myString1 = "banana" // "banana" var possibleInt1 = Int(myString1) //nil print(possibleInt1) // "niln" Printinglet name = "Swift" println("Hello") pringln("My name is (name)") print("See you") print(later) /* Hello My name is Swift See you later */ Logical Operatorsvar happy = true var sad = !happy//logical NOT,sad = false var everyoneHappy = happy && sad//logical AND,everyoneHappy = false var someoneHappy = happy || sad //logical OR,someoneHappy = true Functionsfunc iAdd(a:Int,b:Int,c:Int) -> Int{ return a + b + c } iAdd(1,b: 2,c: 3)//return 6 func eitherSide(n:Int)-> (nMinusOne:Int,nPlusOne:Int){ return(n-1,n+1) } eitherSide(5)//(.0 4,.1 6) Array空数组// Creates an empty array. let emptyArray = [String]() // [] 索引var ratingList = ["Poor","Fine","Good","Excellent"] ratingList[1] = "k" ratingList // return ["Poor","OK","Excellent"] 拼接数组var colors = ["red","blue"] //["red","blue"] var moreColors: [String] = ["orange","purple"] //["orange","purple"] colors.append("green") //["red","blue","green"] colors += ["yellow"] //["red","green","yellow"] colors += moreColors //["red","yellow","orange","purple"] 添加和删除元素var days = ["mon","thu"] var firstDay = days[0] // mon days.insert("tue",atIndex: 1) // [mon,tue,thu] days[2] = "wed" // [mon,wed] days.removeAtIndex(0) //[tue,wed] Dictionaryvar days = ["mon": "monday","tue": "tuseday"] days["tue"] = "tuesday" // change the value for key "tue" days["wed"] = "wednesday" // add a new key/value pair var moreDays: Dictionary = ["thu": "thursday","fri": "friday"] moreDays["thu"] = nil // remove thu from the dictionary moreDays.removeValueForKey("fri") // remove fri from the dictionary (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |