大佬教程收集整理的这篇文章主要介绍了Swift学习笔记——基础语法,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
本文是针对新手学习Swift笔记,记录学习过程的一些体会以及一些学习过程中的理解:
第一次接触Swift或者Oc语法的时候,总感觉有点像脚本语言,不像Java或者C/C++。当然这也只是个人的体会而已。
let maximumnumberOfLoginAttempts = 10
var currentLoginAttempt = 0
var welcome@R_772_8798@ge: String
基本类型
基本类型包含Int,Float,Double,String等类型。
Swift 提供了8, 16, 32和64位的有符号和无符号整数类型(UInt8,Int8; UInt16,Int16; UInt32,Int32; UInt64,Int64)特别的:
浮点类型比整数类型表示的范围更大, 可以存储比Int类型更大或者更小的数字。 Swift 提供了两种有符号浮点数类型:
Double——表示64位浮点数,当你需要存储很大或者很高精度的浮点数时请使用此类型;
Float——表示32位浮点数,精度要求不高的话可以使用此类型。
类型转换
类型转换比较简单,举例子来说:
var a = 3.14; // a is float
var b = Int(a); // b = 3 is Int
typealias AudioSample = UInt16
var maxAmplitudeFound = AudioSample.@H_127_29@min // maxAmplitudeFound = 0
let http404Error = (404,"Not Found")
// http404Error 的类型是 (Int,String), 值是 (404,"Not Found")
let (statusCode,status@R_772_8798@gE) = http404Error
println("The status code is \(statusCodE)")
// 输出 "The status code is 404"
println("The status @R_772_8798@ge is \(status@R_772_8798@gE)")
// 输出 "The status @R_772_8798@ge 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 @R_772_8798@ge is \(http404Error.1)")
// 输出 "The status @R_772_8798@ge is Not Found"
let http200Status = (statusCode: 200,description: "OK")
println("The status code is \(http200Status.statusCodE)")
// 输出 "The status code is 200"
println("The status @R_772_8798@ge is \(http200Status.description)")
// 输出 "The status @R_772_8798@ge 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."
至此基础语法部分差不多完了,其他一些不重要的,或者说其他语言都有的,就没有叙述。下一节将讲述基本运算符。
以上是大佬教程为你收集整理的Swift学习笔记——基础语法全部内容,希望文章能够帮你解决Swift学习笔记——基础语法所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。