问题
I've seen examples of how to resolve this error but I can't get it to work in my case.
Initializer for conditional binding must have Optional type, not 'Int'
Shouldn't employeeCount
be optional since it is inferring the right side?
static func getEmployeeCount() -> Int {
if let employeeCount = UserDefaults.standard.integer(forKey: "employeeCount") {
return employeeCount as! Int
}
return 0
}
回答1:
The problem is UserDefaults.standard.integer return Int
, not Int?
. So you don't need to wrap it with if let
.
The optional
is irrelevant whether on right side.
In your case, just return UserDefaults.standard.integer(forKey: "employeeCount")
来源:https://stackoverflow.com/questions/48555028/how-to-resolve-initializer-for-conditional-binding-must-have-optional-type-not