bạn có thể làm một cái gì đó như thế này, mà có thể hữu ích: (có nghĩa là rất ví dụ chung chung)
enum Country : Int {
case Moldova, Botwana;
//
func capital() -> String {
switch (self) {
case .Moldova:
return "Chișinău"
case .Botwana:
return "Gaborone"
default:
return ""
}
}
//
func flagColours() -> Array<UIColor> {
switch (self) {
case .Moldova:
return [UIColor.blueColor(), UIColor.yellowColor(), UIColor.redColor()]
case .Botwana:
return [UIColor.blueColor(), UIColor.whiteColor(), UIColor.blackColor()]
default:
return []
}
}
//
func all() -> (capital: String, flagColours: Array<UIColor>) {
return (capital(), flagColours())
}
//
var capitolName: String {
get {
return capital()
}
}
//
var flagColoursArray: Array<UIColor> {
get {
return flagColours()
}
}
}
sau đó bạn có thể truy cập đến các chi tiết như thế này:
let country: Country = Country.Botwana
có được vốn
theo cách đó:
let capital: String = country.capital()
này hay cách khác:
let capital: String = country.all().capital
hoặc thứ ba:
let capital: String = country.capitolName
có được màu sắc các cờ của:
theo cách đó:
let flagColours: Array<UIColor> = country.flagColours()
này hay cách khác:
let flagColours: Array<UIColor> = country.all().flagColours
hoặc một phần ba một:
let flagColours: Array<UIColor> = country.flagColoursArray
Đây là những gì tôi đã kết thúc làm, mặc dù tôi không nghĩ rằng đó là lý tưởng, nó mimmic hành vi tôi đang tìm kiếm. –