Swift:没有更多上下文,表达式类型不明确?
Swift: Type of Expression is ambiguous without more context?
查询 Parse 数据库后,我收到错误代码:
if error == nil {
// The find succeeded.
print("Successfully retrieved \(objects!.count) zip codes.", terminator: "")
// Do something with the found objects
if let zipCodes = objects! as? [PFObject] {
if zipCodes.contains({ [=11=]["Zipcode"] as? Int32 == usersZipCode}) { **<-----THIS IS WHERE THE ERROR IS**
print("your in!") // transition to the new screen
self.performSegueWithIdentifier("beginSignUp", sender: self)
}
else {
self.messageLabelNotAvail.text = "Unfortunately, Patch is not available in your area or you have not typed in a correct US Zip Code."
}
}
} else {
// Log details of the failure
print("Error: \(error!) \(error!.userInfo)", terminator: "")
}
}
}
如果我将 Int32 替换为字符串,它工作正常。但是我的解析数据库中的邮政编码是数字而不是字符串。我做错了什么?
而不是:
if zipCodes.contains({ [=10=]["Zipcode"] as? Int32 == usersZipCode}) {
//Rest of Code
}
尝试:
if let target = Int32(usersZipCode)
where zipCodes.contains({ [=11=]["Zipcode"] as? Int32 == target}) {
//Rest of Code
}
Clarification: You can't compare things of different types in Swift. The reason it works when you cast to String
but breaks when you cast to Int32
seems to be that usersZipCode
is of String
type.
查询 Parse 数据库后,我收到错误代码:
if error == nil {
// The find succeeded.
print("Successfully retrieved \(objects!.count) zip codes.", terminator: "")
// Do something with the found objects
if let zipCodes = objects! as? [PFObject] {
if zipCodes.contains({ [=11=]["Zipcode"] as? Int32 == usersZipCode}) { **<-----THIS IS WHERE THE ERROR IS**
print("your in!") // transition to the new screen
self.performSegueWithIdentifier("beginSignUp", sender: self)
}
else {
self.messageLabelNotAvail.text = "Unfortunately, Patch is not available in your area or you have not typed in a correct US Zip Code."
}
}
} else {
// Log details of the failure
print("Error: \(error!) \(error!.userInfo)", terminator: "")
}
}
}
如果我将 Int32 替换为字符串,它工作正常。但是我的解析数据库中的邮政编码是数字而不是字符串。我做错了什么?
而不是:
if zipCodes.contains({ [=10=]["Zipcode"] as? Int32 == usersZipCode}) {
//Rest of Code
}
尝试:
if let target = Int32(usersZipCode)
where zipCodes.contains({ [=11=]["Zipcode"] as? Int32 == target}) {
//Rest of Code
}
Clarification: You can't compare things of different types in Swift. The reason it works when you cast to
String
but breaks when you cast toInt32
seems to be thatusersZipCode
is ofString
type.