对于" homeTeamScore"我有空值(null) Parse后端上的字段。解析字段类型为Number。
但是,下面的条件是评估为TRUE。然后在执行块为TRUE时将其打破。
if (team.objectForKey("homeTeamScore") != nil) {
// execute some code
var homeTeamScoreInt: Int = team.objectForKey("homeTeamScore") as Int! // <- this is where code throws exception
}
我无法弄清楚发生了什么。为什么空值正在评估为TRUE?
以下代码:
println("homeTeamScore")
println(sweet.objectForKey("homeTeamScore"))
打印:
homeTeamScore
<null>
答案 0 :(得分:0)
team.objectForKey("homeTeamScore")
NSNull
使用:
if (team.objectForKey("homeTeamScore") != nil && team.objectForKey("homeTeamScore") is NSNull == false) {
// execute some code
var homeTeamScoreInt: Int = team.objectForKey("homeTeamScore") as Int! // <- this is where code throws exception
}
此示例显示了nil
和NSNull
的打印方式之间的差异:
print(NSNull()) // "<null>"
var a: Int?
print(a) // "nil"