null值正在评估为TRUE

时间:2014-09-26 06:04:43

标签: swift parse-platform

对于" 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>

1 个答案:

答案 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
}

此示例显示了nilNSNull的打印方式之间的差异:

print(NSNull()) // "<null>"
var a: Int?
print(a)        // "nil"