Swift2.0学习三

前端之家收集整理的这篇文章主要介绍了Swift2.0学习三前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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 = 4
let month = Month(rawValue: input)

关联值

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

enum ATMStatus {
    case Success(Int)
    case Error(String)
}

var balance = 1000
func 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都是结构体

原文链接:https://www.f2er.com/swift/323038.html

猜你在找的Swift相关文章