Swift学习笔记——基础语法
发布时间:2020-12-14 01:32:36 所属栏目:百科 来源:网络整理
导读:Swift学习笔记——基础语法 本文是针对新手学习Swift笔记,记录学习过程的一些体会以及一些学习过程中的理解: 基础语法 第一次接触Swift或者Oc语法的时候,总感觉有点像脚本语言,不像Java或者C/C++。当然这也只是个人的体会而已。 常量和变量 比较容易理解
Swift学习笔记——基础语法本文是针对新手学习Swift笔记,记录学习过程的一些体会以及一些学习过程中的理解: 基础语法第一次接触Swift或者Oc语法的时候,总感觉有点像脚本语言,不像Java或者C/C++。当然这也只是个人的体会而已。
let maximumNumberOfLoginAttempts = 10
var currentLoginAttempt = 0
var welcomeMessage: String
var a = 3.14; // a is float
var b = Int(a); // b = 3 is Int
typealias AudioSample = UInt16
var maxAmplitudeFound = AudioSample.min // maxAmplitudeFound = 0
let http404Error = (404,"Not Found")
// http404Error 的类型是 (Int,String), 值是 (404,"Not Found")
let (statusCode,statusMessage) = http404Error
println("The status code is (statusCode)")
// 输出 "The status code is 404"
println("The status message is (statusMessage)")
// 输出 "The status message is Not Found"
let (justTheStatusCode,_) = http404Error
println("The status code is (justTheStatusCode)")
println("The status code is (http404Error.0)")
// 输出 "The status code is 404"
println("The status message is (http404Error.1)")
// 输出 "The status message is Not Found"
let http200Status = (statusCode: 200,description: "OK")
println("The status code is (http200Status.statusCode)")
// 输出 "The status code is 200"
println("The status message is (http200Status.description)")
// 输出 "The status message is OK"
let possibleNumber = "123"
let convertedNumber = possibleNumber.toInt()
// convertedNumber 被推测为类型 "Int?", 或者类型 "optional Int"
if convertedNumber {
println("(possibleNumber) has an integer value of (convertedNumber!)")
} else {
println("(possibleNumber) could not be converted to an integer")
}
// 输出 "123 has an integer value of 123"
if let constantName = someOptional {
statements
}
if let actualNumber = possibleNumber.toInt() {
println("(possibleNumber) has an integer value of (actualNumber)")
} else {
println("(possibleNumber) could not be converted to an integer")
}
// 输出 "123 has an integer value of 123"
var serverResponseCode: Int? = 404
// serverResponseCode 包含一个可选的 Int 值 404
serverResponseCode = nil
// serverResponseCode 现在不包含值
var surveyAnswer: String?
// surveyAnswer 被自动设置为 nil
let possibleString: String? = "An optional string."
println(possibleString!) // 需要惊叹号来获取值
// 输出 "An optional string."
let assumedString: String! = "An implicitly unwrapped optional string."
println(assumedString) // 不需要感叹号
// 输出 "An implicitly unwrapped optional string."
至此基础语法部分差不多完了,其他一些不重要的,或者说其他语言都有的,就没有叙述。下一节将讲述基本运算符。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |