函数中缺少 return - 模型中出现 swift 错误
Missing return in a function - swift error in Model
因此,我在情节提要板上自行生成了饼图,其中包含各种标签、滑块、Age 按钮、CurrentAsset、Income 等。我正在使用 struct 构建模型,并且我不断收到具有 "missing return in a function expected to return 'Double'" 尽管您可以看到我有 returns 用于 currentAssetFactor、savingsFactor、bondFactor、toleranceFactor、outlookFactor。我做错了什么?
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
}
}
您并未涵盖所有情况。考虑您的一种方法:
public var currentAssetFactor: Double {
if currentAsset > maxCurrentAsset {
return 5.0
} else if currentAsset >= minCurrentAsset {
return Double(currentAsset / minCurrentAsset)
}
}
很好。那么如果第一个 if
失败(例如 currentAsset
是 而不是 大于 maxCurrentAsset
)并且第二个 if
失败(例如 currentAsset
是 不是 大于或等于 minCurrentAsset
)?现在我们什么都不 return!
所以你的逻辑有问题。而且编译器很聪明——比你聪明!它 如此 聪明,它发现了您逻辑中的这个漏洞,并阻止您继续前进。您需要以 向编译器证明 涵盖所有可能性的方式重写您的方法,即您将 always return 一个值,无论如何。
因此,我在情节提要板上自行生成了饼图,其中包含各种标签、滑块、Age 按钮、CurrentAsset、Income 等。我正在使用 struct 构建模型,并且我不断收到具有 "missing return in a function expected to return 'Double'" 尽管您可以看到我有 returns 用于 currentAssetFactor、savingsFactor、bondFactor、toleranceFactor、outlookFactor。我做错了什么?
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
}
}
您并未涵盖所有情况。考虑您的一种方法:
public var currentAssetFactor: Double {
if currentAsset > maxCurrentAsset {
return 5.0
} else if currentAsset >= minCurrentAsset {
return Double(currentAsset / minCurrentAsset)
}
}
很好。那么如果第一个 if
失败(例如 currentAsset
是 而不是 大于 maxCurrentAsset
)并且第二个 if
失败(例如 currentAsset
是 不是 大于或等于 minCurrentAsset
)?现在我们什么都不 return!
所以你的逻辑有问题。而且编译器很聪明——比你聪明!它 如此 聪明,它发现了您逻辑中的这个漏洞,并阻止您继续前进。您需要以 向编译器证明 涵盖所有可能性的方式重写您的方法,即您将 always return 一个值,无论如何。