Swift基础学习首页投稿(暂停使用,暂停投稿)

Swift3.0基础部分(The Basics)(二)

2016-07-11  本文已影响90人  Mustard_Buli
类型安全和类型推断(Type Safety and Type Inference)
let meaningOfLife = 42
//meaningOfLife is inferred to be of type Int

当不声明一个浮点型时,默认是Double类型。

let pi = 3.14159
//pi is inferred to be of type Double
let anotherPi = 3 + 0.14159
//anotherPi is also inferred to be of type Double
数字型的字面值(Numeric Literals)

以下这些的值都是17

let decimalInteger = 17
let binaryInteger = 0b10001
let octalInteger = 0o21
let hexadecimalInteger = 0x11
let decimalDouble = 12.1875
let exponentDouble = 1.21875e1
let hexadecimalDouble = 0xC.3p0

numeric literals为了更好的阅读代码可以包含额外的格式,整型和浮点型都可以使用前缀"0"和下划线"_"

let paddedDouble = 000123.456
let oneMillion = 1_000_000
let justOverOneMillion = 1_000_000.000_000_1
数值类型的转换(Numeric Type Conversion)#
let cannotBeNegative: UInt8 = -1
//UInt8 cannot store negative numbers, and so this will report an error.
let tooBig: Int8 = Int8.max + 1
//Int8 cannot store a number larger than its maximum value,
//and so this will also report an error

当两种数值不是一个类型的时候是不能进行运算的,哪怕他们都是“整型”:

let twoThousand: UInt16 = 2_000
let one: UInt8 = 1
let towThousandAndOne = twoThousand + UInt16(one)
//swift的强制转换和OC是不同的,swift是将类型放在括号外面,而将变量(常量)放在括号里面。
let three = 3
let pointOneFourOneFiveNine = 0.141459
let pi = Double(three) + pointOneFourOneFiveNine
// pi equals 3.14159, and is inferred to be of type Double
类型的别称(Type Aliases)

类型的重命名用到的是"typealias"关键字。

typealias AudioSample = UInt16
//correct
let max: AudioSample = AudioSample.max
//wrong
let max: AudioSample = UInt16.max
布尔型(Boolean)

Swift中的布尔型与C语言和OC都不一样,为"Bool",而其值和C语言是一直的,分别为"true"和"false"。
因为type safety的缘故,下面的代码是错误的

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

所以,这段代码应该这么写:

let i = 1
if i == 1 {
    //this example will compile successfully
}

因为"i == 1"的返回值是Bool,所以这段代码可以执行。

元组(Tuples)

元组就是一组多个值合成为一个单一的值,而这一组的多个值并不是一定要相同的类型。
🌰:

let http404Error = (404, "Not Found")
//http404Error is of type (Int, String)

你可以在创建一个元组的时候任意的排序类型。
你也可以在定义一个元组的时候,将类型分离,🌰:

let (statusCode, statusMessage) = http404Error
print("The status code is \(statusCode)")
//Prints "The status code is 404"

如果你只需要为元组中的某个元素命名是,其他的可以用下划线(_)代替:

let (justTheStatusCode, _) = http404Error
print("The status code is \(justTheStatusCode)")
// Prints "The status code is 404"

你也可以通过访问元组的索引值来访问相应的元素:

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"

在定义一个元组的时候,就可以为每个元素命名,这样就可以直接通过元素名称来访问:

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)#

在C语言和OC中是没有optionals的概念的,但在OC中有一个类似的类型就是NSNil。Optionals通俗点讲,就是这个值可能存在,也可能不存在。举个🌰:

let possibleNumber = "123"
let convertedNumber = Int(possibleNumber)
// convertedNumber is inferred to be of type "Int?", or "optional Int"
var serverResponseCode: Int? = 404
// serverResponseCode contains an actual Int value of 404
serverResponseCode = nil
// serverResponseCode now contains no value

在声明一个可选变量的时候不赋给初始值,默认为nil(数值类型的值同样为nil)。

if let actualNumber = Int(possibleNumber) {
        print("\"\(possibleNumber)\" has an integer value of \(actualNumber)")
} else {
        print("\"\(possibleNumber)\" could not be converted to an integer")
}
// Prints ""123" has an integer value of 123"

你可以在一个if判断条件中包含多个可选绑定和"where"子句来进行判断,如果其中有任意一个可选绑定为空(nil),或者"where"的返回值为"false",整个可选绑定都会不成功:

if let firstNumber = Int("4"), secondNumber = Int("42") where irstNumber < secondNumber {
        print("\(firstNumber) < \(secondNumber)")
}
// Prints "4 < 42"
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

同样的,你可以判断隐式解析可选类型是否为空(nil),也可以进行可选绑定。
🌰:

if assumedString != nil {
    print(assumedString)
}
// Prints "An implicitly unwrapped optional string."
if let definiteString = assumedString {
    print(definiteString)
}
// Prints "An implicitly unwrapped optional string."
异常处理(Error Handling)

当一个方法遇到错误情况,它就会抛出(throws)一个错误。而方法的调用者就会捕捉(catch)到错误信息,并及时处理。

func canThrowAnError() throws {
    //this function may or may not throw an error
}

当一个方法可以抛出错误的时候在它声明方法名的时候要包括"throws"关键字。当你调用一个可以抛出错误的方法时,在前面用"try"关键字来修饰。
Swift会自动将错误信息从当先这个范围传递出去,知道会遇到"catch"子句的时候才会进行处理。

do {
    try canThrowAnError()
    //no error was thrown
} catch {
    //an error was thrown
}

"do"状态创建了一个新的可以允许错误信息传递给一个或者多个"catch"子句的包含范围。
🌰:

func makeASandwich() throws {
// ...
}

do {
    try makeASandwich()
    eatASandwich()
} catch SandwichError.outOfCleanDishes {
    washDishes()
} catch SandwichError.missingIngredients(let ingredients) {
    buyGroceries(ingredients)
}

上面这个🌰中,"makeASandewich()"将会在没有干净盘子的时候执行"washDishes()"。之所以"makeASandwich()"可以抛出一个错误,是因为这个方法包含了"try"表达式。如果没有错误抛出,那么"eatASandwich()"这个方法就会执行。

let age = -3
assert(age >= 0, "A person's age cannot be less than zero")
//this causes the assertion to trigger, because age is not >= 0

assertions的信息也可以省略。

assert(age >= 0)
上一篇 下一篇

猜你喜欢

热点阅读