Swift (4.2) 基础语法

2018-10-31  本文已影响104人  小五92

基础语法

一、常量和变量

1、声明

在Swift中用let 声明常量 ,var 声明变量。
全局变量:在函数、方法、闭包或任何类型之外定义的变量。
局部变量:是在函数、方法或闭包内部定义的变量。

let maximumNumberOfLoginAttempts = 10
var currentLoginAttempt = 0
let ceshiStr = "Swift 4.2"

在Swift中变量和常量几乎可以包含任何字符,包括Unicode字符,但不能包含空格、数字、箭头,且不能以数字开头。

2、类型标注

在声明变量和常量的时候,可以通过类型标注,以更清楚变量和常量的类型。

let ceshi : String = "ddd"  //声明中的冒号表示类型标注,冒号后为类型。

也可以在一行上定义相同类型的多个相关变量,以逗号分隔;

var red, green, blue: Double

3、打印

print(_:separator:terminator:)函数打印常量或变量的当前值

二、注释

1、单行注释 用“ // ”

2、多行注释 用“ /* .... */ ”

三、整型

Swift 提供8、16、32和64位格式多有符号和无符号整数。例如:8位无符号整数:UInt8,32位有符号整数:Int32.
其中每个整数类型的最大值和最小值,可调用min 和max来获取。

四、浮点型

Swift 提供来两种带符号的浮点数类型:

五、数字书写格式

六、数字类型转换

Swift中通过 类型名(value)来转换

let three = 3
let pointOneFourOneFiveNine = 0.14159
let pi = Double(three) + pointOneFourOneFiveNine

七、类型别名

Swift中通过typealias关键字定义类型的别名。使用场景是:当希望使用更合适的名称引用现有类型时,可通过别名,在需要使用原来名称的地方使用它。

  typealias AudioSample = UInt16

八、布尔类型

Swift有一个基本的布尔类型,叫做Bool。布尔值被称为逻辑,因为它们只能是true或false。Swift提供了两个布尔常量值,true并且false。

let orangesAreOrange = true
let turnipsAreDelicious = false

if turnipsAreDelicious {
    print("Mmm, tasty turnips!")
} else {
   print("Eww, turnips are horrible.")
}

跟oc 或者 java 不同的是,Swift中,不能 “1” 来替换bool。

let i = 1
if i {
    // this example will not compile, and will report an error
}

九、元组

元组将多个值组合为复合值。元组中的值可以是任何类型,并且不必彼此具有相同的类型。同时,也可将元祖进行拆分。

let http404Error = (404, "Not Found")

//拆分
let (statusCode, statusMessage) = http404Error
print("The status code is \(statusCode)")
// Prints "The status code is 404"
print("The status message is \(statusMessage)")
// Prints "The status message is Not Found"

//如果只想某个值,可使用_ 来忽略其他值。
let (justTheStatusCode, _) = http404Error
print("The status code is \(justTheStatusCode)")
// Prints "The status code is 404"

//也可以使用下标来访问元组中的各个元素值
print("The status code is \(http404Error.0)")
// Prints "The status code is 404"
print("The status message is \(http404Error.1)")
// Prints "The status message is Not Found"

//也可以位各个元组中的成员命名
let http200Status = (statusCode: 200, description: "OK")
print("The status code is \(http200Status.statusCode)")
// Prints "The status code is 200"
print("The status message is \(http200Status.description)")
// Prints "The status message is OK"

//

十、Optionals

声明方法:
var surveyAnswer: String?
通过为其指定特殊值,将可选变量设置为无值状态nil。

一旦确定可选项确实包含值,就可以通过!在可选项名称的末尾添加感叹号来访问其基础值。感叹号有效地说:“我知道这个选项肯定有价值; 请使用它。“这被称为强制解包可选的值:

if convertedNumber != nil {
    print("convertedNumber has an integer value of \(convertedNumber!).")
}
// Prints "convertedNumber has an integer value of 123."

有时,从程序结构中可以清楚地看出,在首次设置该值之后,可选项将始终具有值。在这些情况下,每次访问时都不需要检查和解包可选项的值,因为可以安全地假设它始终具有值。

这些类型的选项被定义为隐式解包的选项。您通过在要使其成为可选的类型之后放置感叹号(String!)而不是问号来编写隐式展开的String?选项。

let possibleString: String? = "An optional string."
let forcedString: String = possibleString! // requires an exclamation mark

let assumedString: String! = "An implicitly unwrapped optional string."
let implicitString: String = assumedString // no need for an exclamation mark

当然也可以将隐式解包的可选项视为普通可选项,以检查它是否包含值:

if assumedString != nil {
    print(assumedString!)
}  // Prints "An implicitly unwrapped optional string."
上一篇下一篇

猜你喜欢

热点阅读