import UIKitvar str = "Hello,playground"//枚举和结构体enum Rank: Int{ case Ace = 1 case Two,Thress,Four,Five,Six,Seven,Eight,Nine,Ten case Jack,Queen,King func simpleDescription()->String{ switch self{ case .Ace: return "ace" case .Jack: return "jack" case .Queen: return "queen" case .King: return "king" default: return String(self.toRaw()) } }}let ace = Rank.Acelet aceRawValue = ace.toRaw()if let convertedRank = Rank.fromraw(3){ let threeDescription = convertedRank.simpleDescription()}enum Suit { case Shades,Hearts,Diamonds,Clubs func simpleDescription()->String{ switch self{ case .Shades: return "shades" case .Hearts: return "hearts" case .Diamonds: return "diamonds" case .Clubs: return "clubs" } }}let hearts = Suit.Heartslet heart = hearts.simpleDescription()struct Card { var rank : Rank var suit : Suit func simpleDescription()->String{ return "The \(rank.simpleDescription()) of \(suit.simpleDescription())" }}let thressofSpades = Card(rank: .Three,suit: .Spades)let threeofSpadesDes = thressofSpades.simpleDescriptionenum ServerResponse{ case Result(String,String) case Error(String)}let success = ServerResponse.Result("6:00 am","8:09 pm")let failure = ServerResponse.Error("out of chees.")switch success{case let .Result(sunrise,sunset): let serverResponse = "Sunrise is at \(sunrise) and sunset is at \(sunset)."case let .Error(error) let serverResponse = "Failure... \(error)"}总结
以上是内存溢出为你收集整理的Swift学习小结之枚举和结构体全部内容,希望文章能够帮你解决Swift学习小结之枚举和结构体所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)