ios - Missing return in a function - swift error in Model -
so have self generated pie chart on storyboard various labels, sliders, buttons of age, currentasset, income, etc. i'm building model struct , keep getting errors several methods have "missing return in function expected return 'double'" though can see have returns currentassetfactor, savingsfactor, bondfactor, tolerancefactor, outlookfactor. doing wrong?
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 } }
you not covering every case. consider 1 of methods:
public var currentassetfactor: double { if currentasset > maxcurrentasset { return 5.0 } else if currentasset >= mincurrentasset { return double(currentasset / mincurrentasset) } }
fine. if first if
fails (e.g. currentasset
not greater maxcurrentasset
) , second if
fails (e.g. currentasset
not greater or equal mincurrentasset
)? don't return anything!
so there's hole in logic. , compiler smart - smarter are! so smart spots hole in logic, , stops in tracks. need rewrite methods in such way prove compiler every possibility covered, i.e. always return value, no matter what.
Comments
Post a Comment