在Swift 3中使用NSNumber和Integer值

罗。

我正在尝试将项目转换为Swift 3.0,但是在使用NSNumber时出现两条错误消息Integers

无法将类型int分配给类型NSNumber

对于

//item is a NSManaged object with a property called index of type NSNumber 

var currentIndex = 0
 for item in self.selectedObject.arrayOfItems {
   item.index = currentIndex
   currentIndex += 1
 }

甚至当我更改currentIndex为一种类型时,也会出现NSNumber错误

二进制运算符'+ ='不能应用于类型'NSNumber'和'Int'

因此,我创建了一个名为onetype 的属性NSNumber以添加到其中,currentIndex但出现以下错误;

二进制运算符'+ ='不能应用于两个NSNumber操作数

&&我得到的第二个错误是

没有'+'候选者产生预期的上下文结果类型NSNumber

 let num: Int = 210
 let num2: Int = item.points.intValue
 item.points = num + num2

在这里,我只是想将210添加到points属性值中,item是一个NSManagedObject

所以基本上我在将数字添加到type属性上时遇到了问题NSNumber我正在与NSNumber他们合作,因为它们是的属性NSManagedObject

谁能帮我吗 ?我有80多个错误,它们都是上述错误之一。

谢谢

马丁·R:

斯威夫特3之前,许多类型为自动“桥接”的一些实例NSObject子类在必要时,例如StringNSString,或者IntFloat......到NSNumber

从Swift 3开始,您必须明确表示该转换:

var currentIndex = 0
for item in self.selectedFolder.arrayOfTasks {
   item.index = currentIndex as NSNumber // <--
   currentIndex += 1
}

或者,在创建NSManagedObject子类时使用选项“对原始数据类型使用标量属性” ,则该属性具有某种整数类型而不是NSNumber,因此无需转换即可获取和设置它。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章