2017-02-11 16 views
1

我是新的,仍在學習。爲了練習,我想嘗試一個想法,看看我是否能夠自己建模。例如,如果我有兩組數據在枚舉中Swift 3.0 - 基於2個不同枚舉的每種情況的調用函數

protocol Pet { 
    var type: PetType { get } 
} 

protocol PetType {} 

enum Cat: PetType { 
    case indoor 
    case outdoor 
} 

enum Dog: PetType { 
    case small 
    case medium 
    case large 
} 

struct MyPet: Pet { 
    let type: PetType 
    let age: Int 
} 

對於每種情況,都有一個特定的功能來計算將要執行的人類年數。所以,如果我創建一個實例

let garfield = myPet(type: Cat.indoor, age: 3) 
myPet.inHumanYears 

應該執行正確的功能。

我嘗試了一些東西,但我的知識似乎現在有限。任何人都可以指導/教導我如何解決這個問題?

謝謝。

回答

1

enum是swift中的一個強大類型。真正很酷的是它可以具有取決於哪個功能和屬性的情況。通過添加一些屬性的枚舉你可以做你想要的。

您的代碼將是這個樣子:

protocol Pet { 
    var type: PetType { get } 
} 

protocol PetType { 
    func inHumanYears(age: Int) -> Int 
} 

enum Cat: PetType { 
    case indoor 
    case outdoor 

    func inHumanYears(age: Int) -> Int { 
    switch self { 
    case .indoor: 
     //return you calculations for indoor 
    case .outdoor: 
     //return you calculations for outdoor 
    } 
    } 
} 

enum Dog: PetType { 
    case small 
    case medium 
    case large 

    func inHumanYears(age: Int) -> Int { 
    switch self { 
    case .small: 
     //return you calculations for small 
    case .large: 
     //return you calculations for large 
    case .medium: 
     //return you calculations for medium 
    } 
    } 
} 

struct MyPet: Pet { 
    let type: PetType 
    let age: Int 

    var inHumanYears: Int { 
    return type.inHumanYears(age: age) 
    } 
} 

PetType檢查哪個枚舉它本身並據此計算的情況下。那麼你可以這樣做:

let garfield = MyPet(type: Cat.indoor, age: 3) 
print(garfield.inHumanYears) 

希望它有幫助!

+0

這沒有幫助,謝謝。我可以在每種情況下使用if語句,否則我只能返回一個值。因爲取決於年齡範圍,因此每種情況下的計算都會有所不同。 –

+0

當然可以。你可以在每種情況下做任何你想做的事情,比如進一步的邏輯。 –

+0

這工作得很好:)只是出於好奇,這是做到這一點的最佳方式?如果使用其他方法進行計算,還是有更多方法可以做到這一點?我有很多要學習的。 –

0

有幾種方法。如果人類的年代是獨一無二的(即,室外的貓永遠不會像室內的貓一樣),那麼你可以擴展Int並使用rawValue。否則,你與你的approaach是對的,你可以這樣做:

獨特的人類年

import UIKit 

protocol Pet { 
    var type: PetType { get } 
} 

protocol PetType { 
    var humanYears: Int { get} 
} 

enum Cat: Int, PetType { 
    case indoor = 1 
    case outdoor = 10 

    var humanYears: Int { 
     return self.rawValue 
    } 
} 

enum Dog: Int, PetType { 
    case small = 40 
    case medium = 1 
    case large = 4 

    var humanYears: Int { 
     return self.rawValue 
    } 

} 

struct MyPet: Pet { 
    let type: PetType 
    let age: Int 
} 

let garfield = MyPet(type: Cat.indoor, age: 3) 
garfield.type.humanYears 

複製人類年

import UIKit 

protocol Pet { 
    var type: PetType { get } 
} 

protocol PetType { 
    var humanYears: Int { get} 
} 

enum Cat: PetType { 
    case indoor 
    case outdoor 

    var humanYears: Int { 
     switch self { 
     case .indoor: return 4 
     case .outdoor: return 5 
     } 
    } 
} 

enum Dog: PetType { 
    case small 
    case medium 
    case large 

    var humanYears: Int { 
     switch self { 
     case .small: 
      if (yearIs1960) { 
       return 10 
      } 
      else { 
       return 8 
      } 
     case .medium: return 5 
     case .large: return 5 
     } 
    } 
} 

struct MyPet: Pet { 
    let type: PetType 
    let age: Int 
} 

let garfield = MyPet(type: Cat.indoor, age: 3) 
garfield.type.humanYears 

這裏是一個非常受貝內迪克特Terhechte good article即進入深度與枚舉的可能性的例子

+0

謝謝!我認爲這兩個想法對我有很大的幫助,我希望在每個案例中都有一個if else的陳述,因爲年齡可以是任何事情。 –

+0

我已經更新了我的狗的enum文章,以展示你如何做到這一點。 –