2015-04-22 80 views
0

因此我在故事板上自動生成餅圖,其中包含Age,CurrentAsset,Income等各種標籤,滑塊,按鈕等。我使用struct構建模型,並保留即使你可以看到我有currentAssetFactor,savingsFactor,bondFactor,toleranceFactor,outlookFactor的返回值,也會出現幾個方法的錯誤,這些方法的「缺少希望返回的函數返回'Double''。我究竟做錯了什麼?在函數中缺少返回 - 模型

public struct AllocationModel 
{ 
    let maxAge = 95.0 
    let currentAge: Double 

    let minCurrentAsset = 100000.0 
    let maxCurrentAsset = 500000.0 
    let currentAsset: Double 

    let minSavings = 5000.0 
    let maxSavings = 20000.0 
    let currentSavings: Double 

    let maxIncomeRequired = 0.04 
    let minIncomeRequired = 0.01 
    let currentIncomeRequired: Double 

    let tolerance: String 

    let outlook: String 

    // MARK: - Methods 

    public var currentAssetFactor: Double { 
    if currentAsset > maxCurrentAsset { 
     return 5.0 
    } else if currentAsset >= minCurrentAsset { 
     return Double(currentAsset/minCurrentAsset) 
    } 
    } 

    public var savingsFactor: Double { 
    if currentSavings >= maxSavings { 
     return 4.0 
    } else if currentSavings >= minSavings { 
     return Double(currentSavings/minSavings) 
    } 
    } 

    public var bondFactor: Double { 
    if currentIncomeRequired >= maxIncomeRequired { 
     return 0.2 
    } else if currentIncomeRequired >= minIncomeRequired { 
     return (0.05 * Double(currentIncomeRequired/minIncomeRequired)) 
    } 
    } 

    public var toleranceFactor: Double { 
    switch tolerance { 
     case "conservative": return 0.0 
     case "balanced": return 3.0 
     case "aggressive": return 6.0 
    default: 0.0 
    } 
    } 

    public var outlookFactor: Double { 
    switch outlook { 
     case "poor": return 0.0 
     case "moderate": return 7.0 
     case "strong": return 10.0 
    default: return 0.0 
    } 
    } 


    public var stock: Double { 
    return maxAge - currentAge + currentAssetFactor + savingsFactor + toleranceFactor + outlookFactor 
    } 

    public var nonStock: Double { return 100.0 - stock } 

    public var bondPercentage: Double { return 0.35 + bondFactor } 

    public var bond: Double { return nonStock * bondPercentage } 

    public var cash: Double { return nonStock - bond } 

    // MARK: - Init 

    public init(currentAge: Double, currentAsset: Double, currentSavings: Double, currentIncomeRequired: Double, tolerance: String, outlook: String) { 
    assert(currentAge > 0) 

    self.currentAge = currentAge 
    self.currentAsset = currentAsset 
    self.currentSavings = currentSavings 
    self.currentIncomeRequired = currentIncomeRequired 
    self.tolerance = tolerance 
    self.outlook = outlook 
    } 

} 

回答

1

你沒有涵蓋每一個案例。考慮你的方法之一:

public var currentAssetFactor: Double { 
    if currentAsset > maxCurrentAsset { 
     return 5.0 
    } else if currentAsset >= minCurrentAsset { 
     return Double(currentAsset/minCurrentAsset) 
    } 
    } 

好。那麼,如果所述第一if失敗(例如currentAsset不大於maxCurrentAsset更大)和第二if失敗(例如currentAsset大於或等於minCurrentAsset)?現在我們不回報任何東西!

所以你的邏輯有一個漏洞。編譯器很聰明 - 比你更聰明!這是所以聰明,它在你的邏輯中發現這個洞,並阻止你在你的軌道上。你需要以這樣的方式重寫你的方法,以便證明給編譯器,覆蓋了每一種可能性,也就是說你會總是返回一個值,不管是什麼。

+1

您應該考慮將此作爲案例陳述書寫......它將幫助您找出遺漏的案例 – Jeef

+0

添加到此答案中,在'outlookFactor'默認值:在交換機內部,您忘記了'return 0.0' –

+0

@Jeef very好點,我正在考慮補充。 – matt