大佬教程收集整理的这篇文章主要介绍了Swift Cheat Sheet [1] — Basic Types,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
var 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
let 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来表示它没有值.
var serverResponseCode: Int? = 404 // serverResponseCode 包含一个可选的 Int 值 404 serverResponseCode = nil // serverResponseCode 现在不包含值
如果你声明一个可选常量或者变量但是没有赋值,它们会自动被设置为nil
var surveyAnswer: String? // surveyAnswer 被自动设置为 nil
注意:
使用if语句和nil比较来判断一个可选值是否包含值
当你确定可选类型确实包含值之后,你可以在可选的名字后面加一个感叹号(!)来获取值
var convertednumber : Int? = 10 if convertednumber != nil{ print("convertednumber has an Integer value of \(convertednumber!)") } // 输出 "convertednumber has an Integer value of 10"
使用可选绑定(optional binding)来判断可选类型是否包含值,如果包含就把值赋给一个临时常量或者变量。可选绑定可以用在if和while语句中,这条语句不仅可以用来判断可选类型中是否有值,同时可以将可选类型中的值赋给一个常量或者变量
示例:
let possiblenumber = “123”
if let actualnumber = Int(possiblenumber){
print(“\’(possiblenumber)\’ has an Integer value of (actualnumber)”)
}else{
print(“\’(possiblenumber)\’ Could not be convered to an Integer”)
}
解释这个示例:
如果Int(possiblenumber)返回的可选Int包含一个值,创建一个叫做actualnumber的新常量并将可选包含的值赋给它。
如果转换成功,actualnumber常量可以在if语句的第一个分支中使用。它已经被可选类型包含的值初始化过,所以不需要再使用!后缀来获取它的值。在这个例子中,actualnumber只被用来输出转换结果。
if let firstnumber = Int("4"),secondnumber = Int("42") where firstnumber < secondnumber { print("\(firstnumber) < \(secondnumber)") } // prints "4 < 42"
在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)
let minValue = UInt8.min // minValue 为 0,是 UInt8 类型 let maxValue = UInt8.max // maxValue 为 255,是 UInt8 类型
var myString = "a" let myImmutableString = "c" myString += "b" // ab myString = myString + myImmutableString //abc myImmutableString += "d" //compile-time error!!!
let count = 7 let message = "There are \(count) days in a week"
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
let three = 3 let pointOneFourOneFiveNine = 0.14159 let pi = Double(threE) + pointOneFourOneFiveNine // pi 等于 3.14159,所以被推测为 Double 类型
let IntegerPi = Int(pi) // IntegerPi 等于 3,所以被推测为 Int 类型
let label = "The width is" let width = 94 let widthLabel = label + String(width)// The width is 94
code1:
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) // "nil\n"
let name = "Swift" println("Hello") pringln("My name is \(Name)") print("See you") print(later) /* Hello My name is Swift See you later */
var 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
func 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)
// 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]
var 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
以上是大佬教程为你收集整理的Swift Cheat Sheet [1] — Basic Types全部内容,希望文章能够帮你解决Swift Cheat Sheet [1] — Basic Types所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。