The Swift Programming Language (Swift 2) -4 Enumerations and Structures
21 September 2015
//: ## Enumerations and Structures//://: Use `enum` to create an enumeration. Like classes and all other named types, enumerations can have methods associated with them.//:enumRank:Int{caseAce=1caseTwo,Three,Four,Five,Six,Seven,Eight,Nine,TencaseJack,Queen,KingfuncsimpleDescription()->String{switchself{case.Ace:return"ace"case.Jack:return"jack"case.Queen:return"queen"case.King:return"king"default:returnString(self.rawValue)}}}letace=Rank.AceletaceRawValue=ace.rawValue//: > **Experiment**://: > Write a function that compares two `Rank` values by comparing their raw values.//://: In the example above, the raw-value type of the enumeration is `Int`, so you only have to specify the first raw value. The rest of the raw values are assigned in order. You can also use strings or floating-point numbers as the raw type of an enumeration. Use the `rawValue` property to access the raw value of an enumeration member.//://: Use the `init?(rawValue:)` initializer to make an instance of an enumeration from a raw value.//:ifletconvertedRank=Rank(rawValue:3){letthreeDescription=convertedRank.simpleDescription()}//: The member values of an enumeration are actual values, not just another way of writing their raw values. In fact, in cases where there isn’t a meaningful raw value, you don’t have to provide one.//:enumSuit{caseSpades,Hearts,Diamonds,ClubsfuncsimpleDescription()->String{switchself{case.Spades:return"spades"case.Hearts:return"hearts"case.Diamonds:return"diamonds"case.Clubs:return"clubs"}}}lethearts=Suit.HeartsletheartsDescription=hearts.simpleDescription()//: > **Experiment**://: > Add a `color()` method to `Suit` that returns “black” for spades and clubs, and returns “red” for hearts and diamonds.//://: Notice the two ways that the `Hearts` member of the enumeration is referred to above: When assigning a value to the `hearts` constant, the enumeration member `Suit.Hearts` is referred to by its full name because the constant doesn’t have an explicit type specified. Inside the switch, the enumeration member is referred to by the abbreviated form `.Hearts` because the value of `self` is already known to be a suit. You can use the abbreviated form anytime the value’s type is already known.//://: Use `struct` to create a structure. Structures support many of the same behaviors as classes, including methods and initializers. One of the most important differences between structures and classes is that structures are always copied when they are passed around in your code, but classes are passed by reference.//:structCard{varrank:Rankvarsuit:SuitfuncsimpleDescription()->String{return"The \(rank.simpleDescription()) of \(suit.simpleDescription())"}}letthreeOfSpades=Card(rank:.Three,suit:.Spades)letthreeOfSpadesDescription=threeOfSpades.simpleDescription()//: > **Experiment**://: > Add a method to `Card` that creates a full deck of cards, with one card of each combination of rank and suit.//://: An instance of an enumeration member can have values associated with the instance. Instances of the same enumeration member can have different values associated with them. You provide the associated values when you create the instance. Associated values and raw values are different: The raw value of an enumeration member is the same for all of its instances, and you provide the raw value when you define the enumeration.//://: For example, consider the case of requesting the sunrise and sunset time from a server. The server either responds with the information or it responds with some error information.//:enumServerResponse{caseResult(String,String)caseError(String)}letsuccess=ServerResponse.Result("6:00 am","8:09 pm")letfailure=ServerResponse.Error("Out of cheese.")switchsuccess{caselet.Result(sunrise,sunset):letserverResponse="Sunrise is at \(sunrise) and sunset is at \(sunset)."caselet.Error(error):letserverResponse="Failure... \(error)"}//: > **Experiment**://: > Add a third case to `ServerResponse` and to the switch.//://: Notice how the sunrise and sunset times are extracted from the `ServerResponse` value as part of matching the value against the switch cases.//://: [Previous](@previous) | [Next](@next)