iOS二次發育(swift)枚舉

來源:互聯網
上載者:User

標籤:col   can   ace   point   cas   erro   oct   logs   sid   

//: Playground - noun: a place where people can playimport UIKit//Raw Valueenum Month: String{    case January = "Value_January"    case February = "Value_February"    case March = "Value_March"    case April = "Value_April"    case May = "Value_May"    case June = "Value_June"    case July = "Value_July"    case August = "Value_August"    case September = "Value_September"    case October = "Value_October"    case November = "Value_November"    case December = "Value_December"}func season(month: Month ) -> String{    switch month {    case .January:        return month.rawValue    default:        return ""    }}season(month: Month.January)print(Month.April)if let july = Month(rawValue: "Value_July"){   print(july)}//Associate Value//例1enum ATMStatus{    case Success(Int)    case Error(String)    case Waiting}var balance = 1000func withdraw( amount: Int ) -> ATMStatus{    if amount <= balance {        balance -= amount        return .Success(balance)    } else {        return .Error("Not enough money")    }}let result = withdraw(amount: 800)switch result {    case let .Success(newBalance):        print("\(newBalance) Yuan Left in your account.")    case let .Error(errorMessage):        print("error:\(errorMessage)")    case .Waiting:        print("Waiting")}switch result {    case .Success:        print("Success")    case .Error:        print("Error")    case .Waiting:        print("Waiting")}//例2enum Shape{    case Square(side: Double)    case Rectangle(width: Double, height: Double)    case Circle(centerX: Double, centerY: Double, radius: Double)    case Point}let square = Shape.Square(side: 10)let rectangle = Shape.Rectangle(width: 20, height: 30)let circle = Shape.Circle(centerX: 0, centerY: 0, radius: 15)let point = Shape.Pointfunc area(shape: Shape) -> Double{    switch shape {    case let .Square(side):        return side * side    case let .Rectangle(width, height):        return width * height    case let .Circle(_, _, r):        return M_PI * r * r    case .Point:        return 0    }}area(shape: square)area(shape: rectangle)area(shape: circle)area(shape: point)

 

iOS二次發育(swift)枚舉

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.