为什么Swift中的可选项给我EXC_BAD_ACCESS?

Ric*_*opo -1 exc-bad-access ios swift

我有以下代码:

// Get the matches
var recipieMatches = queryResults[MATCHES_INDEX] as Array<Dictionary<String, AnyObject>>
// Set some global vars
var numberOfRecipiesToDisplay: Int = recipieMatches.count

for i in 0...(numberOfRecipiesToDisplay - 1)
{
    var time:  Int? = ((recipieMatches[i])["totalTimeInSeconds"]) as Int? // THIS LINE
}
Run Code Online (Sandbox Code Playgroud)

我在for循环中的行上得到一个EXC_BAD_ACCESS错误.

我的问题:这怎么可能?我知道recipieMatches[i]是一个值,因为for循环遍历数组的计数.

Aar*_*ger 5

as 如果您提取的值不是正确的类型将崩溃 - 在这种情况下,如果它不是 Int?

相反,使用可选的铸造 - as?.如果该值不是a,Int?那么可选的将是nil.