Swift2.0学习三

Swift2.0学习三,第1张

概述Swift2.0学习三 枚举 在Swift中枚举值可以是任何类型 可选型的实质是枚举。 var webSite: Optional<String> = Optional.Some("imooc.com")webSite = .None 原始值Raw Value enum Month: Int { case January = 1 case February = 2 cas Swift2.0学习三 枚举

在Swift中枚举值可以是任何类型

可选型的实质是枚举。

var webSite: Optional<String> = Optional.some("imooc.com")webSite = .None
原始值Raw Value
enum Month: Int {    case January = 1    case February = 2    case march = 3    case April = 4    case May = 5    case June = 6    case July = 7    case August = 8    case September = 9    case October = 10    case November = 11    case December = 12}func monthsBeforeNewYear( month: Month ) -> Int {    return 12 - month.rawValue}

根据rawValue构建枚举,返回值是一个可选型

let input = 4let month = Month(rawValue: input)
关联值

在枚举中,每一种可能性可以和一种变量相连接。

enum ATMStatus {    case Success(Int)    case Error(String)}var balance = 1000func withdraw( amount: Int ) -> ATMStatus {    if balance >= amount {        balance -= amount        return .Success(balance)    }else {        return .Error("Not enough money")    }}let result = withdraw(100)switch result {case let .Success(newBalance):    print("\(newBalance) Yuan left in your account.")case let .Error(errorMessage):    print("Error: \(errorMessage)")}
递归枚举

使用indirect表示递归枚举

indirect enum ArithmeticExpression {    case Number(Int)    case Addition( ArithmeticExpression,ArithmeticExpression )    case Multiplication( ArithmeticExpression,ArithmeticExpression )}

使用

let five = ArithmeticExpression.Number(5)let four = ArithmeticExpression.Number(4)let sum = ArithmeticExpression.Addition(five,four)let two = ArithmeticExpression.Number(2)let product = ArithmeticExpression.Multiplication(sum,two)func evaluate(Expression: ArithmeticExpression) -> Int {    switch Expression {    case let .Number(value):        return value    case let .Addition(left,right):        return evaluate(left) + evaluate(right)    case let .Multiplication(left,right):        return evaluate(left) * evaluate(right)}evaluate(product) //18
结构体

结构体中声明的常量不可改变
构造函数
给结构体设置储值,可以如下创建结构体

struct Location{    var latitude: Double = 0    var longitude: Double = 0}var location = Location()

使用init()函数,重写构造函数

struct Location{    let latitude: Double    let longitude: Double    init(coordinateString: String) {        let commaIndex = coordinateString.rangeOfString(",")!.startIndex        let firstElement = coordinateString.substringToIndex(commaIndex)        let secondElement = coordinateString.substringFromIndex(commaIndex.successor())        latitude = Double(firstElement)!        longitude = Double(secondElement)!    }}

可以使用如下方式构造结构体

let appleheadQuarterLocation = Location(coordinateString: "37.2,47.8")

此时默认的构造方法就无效了。

可失败的构造函数

init?(coordinateString: String) {    guard let commaIndex = coordinateString.rangeOfString(",")?.startIndex else{        return nil    }    guard let firstElement = Double(coordinateString.substringToIndex(commaIndex)) else{        return nil    }    guard let secondElement = Double(coordinateString.substringFromIndex(commaIndex.successor())) else {        return nil    }    self.latitude = firstElement    self.longitude = secondElement}

结构体和枚举是值类型 在Swift中Array Dictionary Set String Int float Double Bool都是结构体

总结

以上是内存溢出为你收集整理的Swift2.0学习三全部内容,希望文章能够帮你解决Swift2.0学习三所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1072357.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存