Reputation: 310
I'm trying to make a call from my JSON file where the variable is a string however in order to compare it I would want it to be an integer, however, whenever I try and convert it using methods on here nothing seems to be working, assuming the wrong syntax. This line essentially (pData.info?.nutriScore ?? 0) prints a score however its a string.
if let nScore = Int(pData.info?.myScore ?? 0) < 0 {
//Other Code
}
Upvotes: 1
Views: 1620
Reputation: 193
Avoid using ?? default value ,
Yes you dont have the value in your object so you are passing the default that doesnt mean default value is your Real data .
if let b = pData.info?.myScore, let nScore = Int(b) , nScore >= 0{
print(nScore)
} else {// handle negative logic}
Upvotes: 1
Reputation: 100503
You need
if let nScore = Int(pData.info?.myScore ?? "0" ) , nScore > 0 {
}
Upvotes: 1
Reputation: 183
if let nScore:Int = Int(pData.info?.nutriScore ?? "0") {
if nScore < 0 {
print(nScore)
}
}
Upvotes: 1
Reputation: 446
if let nutriScore = pData.info?.nutriScore, let nScore = Int(nutriScore) {
// your code here
}
Upvotes: 2