无法从NSArrayM获得号码

时间:2017-01-08 22:18:12

标签: ios arrays swift int

尝试从查询中检索密钥名称时出现以下错误。然后将它转换为viewcontroller使用的int。

Could not cast value of type '__NSArrayM' (0x1073ebdb0) to 'NSNumber' (0x1069f3488).

这是查询中的一行:

tempVarLDS = object.value(forKey: keyName)

打印时返回

(
  7
)

所以它是一个数组。但我似乎无法通过

访问它
tempvarLDS[0]

或任何通常的嫌疑人。任何想法如何获得这个int?

1 个答案:

答案 0 :(得分:1)

如果它是NSNumber的NSArray,你应该能够将它作为整数数组投射。

// Cast it
let tempvarLDS:[Int] = object.value(forKey: keyName) as! [Int]

// Loop
for num in tempvarLDS {
    print(num)
}

我不明白为什么那不起作用

相关问题