美文网首页
Day1语法基础

Day1语法基础

作者: 平凡之路561 | 来源:发表于2017-05-31 19:17 被阅读0次

    数据类型

    Swift 用字符串插值(string interpolation)的方式把常量名或者变量名当做占位符加入到长字符串中,Swift 会用当前常量或变量的值替换这些占位符。将常量或变量名放入圆括号中,并在开括号前使用反斜杠将其转义:
    <pre>print("The current value of friendlyWelcome is (friendlyWelcome)”)
    // 输出 "The current value of friendlyWelcome is Bonjour!</pre>

    整数
    就是没有小数部分的数字,比如 42 和 -23 。整数可以是 有符号 (正、负、零)或者 无符
    号 (正、零)。
    Swift 提供了8,16,32和64位的有符号和无符号整数类型。这些整数类型和 C 语言的命名方式很像,比如8位无 符号整数类型是 UInt8 ,32位有符号整数类型是 Int32 。就像 Swift 的其他类型一样,整数类型采用大写命名法。

    整数范围
    你可以访问不同整数类型的 min 和 max 属性来获取对应类型的最小值和最大值:
    <pre>let minValue = UInt8.min // minValue 为 0,是 UInt8 类型
    let maxValue = UInt8.max // maxValue 为 255,是 UInt8 类型</pre>

    浮点数
    浮点数是有小数部分的数字,比如 3.14159 ,0.1 和 -273.15。
    浮点类型比整数类型表示的范围更大,可以存储比 Int 类型更大或者更小的数字。Swift 提供了两种有符号浮点数类型:
    • Double
    表示64位浮点数。当你需要存储很大或者很高精度的浮点数时请使用此类型。

    • Float
    表示32位浮点数。精度要求不高的话可以使用此类型。

    类型别名
    类型别名(type aliases)就是给现有类型定义另一个名字。你可以使用 typealias 关键字来定义类型别名。
    当你想要给现有类型起一个更有意义的名字时,类型别名非常有用。假设你正在处理特定长度的外部资源的数据:typealias AudioSample = UInt16
    定义了一个类型别名之后,你可以在任何使用原始名的地方使用别名:
    <pre>var maxAmplitudeFound = AudioSample.min
    // maxAmplitudeFound 现在是 0</pre>
    本例中, AudioSample 被定义为 UInt16 的一个别名。因为它是别名, AudioSample.min 实际上是 UInt16.min ,所以会给 maxAmplitudeFound 赋一个初值 0 。

    元组
    元组(tuples)把多个值组合成一个复合值。元组内的值可以是任意类型,并不要求是相同类型。
    下面这个例子中, (404, "Not Found") 是一个描述 HTTP 状态码(HTTP status code)的元组。HTTP 状态码是 当你请求网页的时候 web 服务器返回的一个特殊值。如果你请求的网页不存在就会返回一个 404 Not Found 状 态码。

    <pre>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"</pre>

    如果你只需要一部分元组值,分解的时候可以把要忽略的部分用下划线( _ )标记:
    <pre>
    let (justTheStatusCode, _) = http404Error
    print("The status code is (justTheStatusCode)")
    // 输出 "The status code is 404"</pre>
    此外,你还可以通过下标来访问元组中的单个元素,下标从零开始:
    <pre>
    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"</pre>

    你可以在定义元组的时候给单个元素命名:
    <pre>
    let http200Status = (statusCode: 200, description: "OK")</pre>
    给元组中的元素命名后,你可以通过名字来获取这些元素的值:
    <pre>
    print("The status code is (http200Status.statusCode)")
    // 输出 "The status code is 200"
    print("The status message is (http200Status.description)")
    // 输出 "The status message is OK”</pre>

    可选类型
    使用可选类型(optionals)来处理值可能缺失的情况。可选类型表示:
    • 有值,等于 x或者
    • 没有值

    下面的例子使用这种构造器来尝试将一个 String 转换成 Int:
    <pre>let possibleNumber = "123"
    let convertedNumber = Int(possibleNumber)</pre>

    // convertedNumber 被推测为类型 "Int?", 或者类型 "optional Int"因为该构造器可能会失败,所以它返回一个可选类型(optional) Int ,而不是一个 Int 。
    一个可选的 Int 被写作 Int? 而不是 Int 。问号暗示包含的值是可选类型,也就是说可能包含 Int 值也可能不包含值。(不能包含其他任何值比如 Bool 值或者 String 值。只能是 Int 或者什么都没有。)

    nil
    你可以给可选变量赋值为 nil 来表示它没有值:

    <pre>var serverResponseCode: Int? = 404
    // serverResponseCode 包含一个可选的 Int 值 404 serverResponseCode = nil
    // serverResponseCode 现在不包含值</pre>

    注意:nil 不能用于非可选的常量和变量。如果你的代码中有常量或者变量需要处理值缺失的情况,请把它们声明成对应的可选类型。
    如果你声明一个可选常量或者变量但是没有赋值,它们会自动被设置为 nil :
    <pre>
    var surveyAnswer: String?
    // surveyAnswer 被自动设置为 nil</pre>

    注意:

    Swift 的 nil 和 Objective-C 中的 nil 并不一样。
    在 Objective-C 中, nil 是一个指向不存在对象的指针。
    在 Swift 中, nil 不是指针——它是一个确定的值,用来表示值缺失。任何类型的可选状态都可以设置为 nil ,不只是对象类型。

    相关文章

      网友评论

          本文标题:Day1语法基础

          本文链接:https://www.haomeiwen.com/subject/iimjfxtx.html