我正在将我的项目转换为
Swift 3.0,但是在使用NSNumber和Integer时,我有两个错误消息.
Cannot assign type int to type 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类型,那么我得到错误
Binary operator ‘+=’ cannot be applied to type ‘NSNumber’ and ‘Int’
所以我创建一个名为NSNumber类型的属性来添加到currentIndex,但是会得到以下错误;
Binary operator ‘+=’ cannot be applied to two NSNumber operands
&安培;&安培;我得到的第二个错误是
No ‘+’ candidates produce the expected contextual result type NSNumber
let num: Int = 210 let num2: Int = item.points.intValue item.points = num + num2
这里我只是试图添加210到点属性值,item是一个NSManagedObject.
所以基本上我有问题让我的头围绕NSNumber类型的属性添加数字.我正在使用NSNumber,因为它们是NSManagedObject的属性.
谁能帮我吗 ?我有超过80个错误,都是上述错误之一.
谢谢
解决方法
在Swift 3之前,许多类型被自动“桥接”到一个
某些NSObject子类的实例在必要的情况下,例如String to
NSString,或Int,Float,…到NSNumber.
某些NSObject子类的实例在必要的情况下,例如String to
NSString,或Int,Float,…到NSNumber.
从Swift 3开始,您必须明确表示:
var currentIndex = 0 for item in self.selectedFolder.arrayOfTasks { item.index = currentIndex as NSNumber // <-- currentIndex += 1 }
或者,在创建NSManagedObject子类时,使用选项“使用原始数据类型的标量属性”那么该属性有一些整数类型而不是NSNumber,以便您无需转换即可获得和设置.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。