zoukankan      html  css  js  c++  java
  • S(tuple)类及可选(Optional)类型型

    元组将多个值组合为单个值。元组内的值可以是任意 类型,各元素不必是相同的类型。元组在作为函数返 回值时尤其有用。

    1、定义方法1

    let http404Error= (404,"Not Found")

    println("The status codeis (http404Error.0)")

    // prints "The status codeis 404"

    println("The statusmessage is (http404Error.1)")

    // prints "The status message isNot Found"

    2、定义方法2

    let http200Status = (statusCode: 200, description: "OK")

    println("The          status          code           is

    (http200Status.statusCode)")

    // prints "The status codeis 200"

    println("The         status         message           is

    (http200Status.description)")

    // prints "The status message isOK"

    可选(Optional)类型

    使用可选类型

    我们在如下情况下使用可选类型:

    •   它有值但不确定

    •   没有任何值

    let possibleNumber = "123" //Hello

    let convertedNumber : Int? = possibleNumber.toInt()"Int?"是可选类型

    if convertedNumber {

    println("(possibleNumber) has an integer value of

    (convertedNumber!)")

    } else {

    println("(possibleNumber) could not be convertedtoan integer")

    }

    convertedNumber!是从可选类型中取值。

    使用 nil

    我们可以为可选类选的变量设置 nil 值,表示没有任何 值。

    var serverResponseCode: Int? = 404

    // serverResponseCode contains an actual Int value of

    404

    serverResponseCode = nil

    // serverResponseCode now containswift元组

    no value

     Swift交流讨论论坛论坛:http://www.cocoagame.net
    欢迎加入Swift技术交流群:362298485

  • 相关阅读:
    (转)jqgrid 显示格式化数据
    (转)JS 中如何将字符串转化成日期&&日期格式化方法
    (转)PowerDesigner 12.5下载
    SQL DATEADD语法
    Javascript 版本utf8转换为gb2312
    解决“Lc.exe 已退出,代码 1”错误
    Oracle多表关联更新
    ORACLE合并查询结果
    DataGrid 格式化字段
    动态类帮助器
  • 原文地址:https://www.cnblogs.com/iOS-Blog/p/3792303.html
Copyright © 2011-2022 走看看