ios – 在Swift 3中使用NSNumber和Integer值

ios – 在Swift 3中使用NSNumber和Integer值,第1张

概述我正在将我的项目转换为 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 我正在将我的项目转换为 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.

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

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

或者,在创建NSManagedobject子类时,使用选项“使用原始数据类型的标量属性”那么该属性有一些整数类型而不是NSNumber,以便您无需转换即可获得和设置.

总结

以上是内存溢出为你收集整理的ios – 在Swift 3中使用NSNumber和Integer值全部内容,希望文章能够帮你解决ios – 在Swift 3中使用NSNumber和Integer值所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1111456.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存