ios 在 Swift 3 中使用 NSNumber 和整数值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39321421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Working with NSNumber & Integer values in Swift 3
提问by A.Roe
I am trying to convert my project to Swift 3.0 however I am having two error messages when working with NSNumber
and Integers
.
我正在尝试将我的项目转换为 Swift 3.0,但是在使用NSNumber
和时出现两条错误消息Integers
。
Cannot assign type int to type NSNumber
无法将类型 int 分配给类型 NSNumber
for
为了
//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
}
and even when I change currentIndex
to a type NSNumber
then I get the error
甚至当我更改currentIndex
为一种类型时,我也会NSNumber
收到错误消息
Binary operator '+=' cannot be applied to type 'NSNumber' and 'Int'
二元运算符“+=”不能应用于类型“NSNumber”和“Int”
so then I create a property called one
of type NSNumber
to add to currentIndex
but then get the following error;
所以然后我创建了一个名为one
type的属性NSNumber
来添加,currentIndex
但随后出现以下错误;
Binary operator '+=' cannot be applied to two NSNumber operands
二元运算符“+=”不能应用于两个 NSNumber 操作数
&& the second error I get is
&& 我得到的第二个错误是
No '+' candidates produce the expected contextual result type NSNumber
没有“+”候选产生预期的上下文结果类型 NSNumber
let num: Int = 210
let num2: Int = item.points.intValue
item.points = num + num2
Here I am just trying to add 210 to the points property value, item
is a NSManagedObject
.
在这里,我只是想将 210 添加到 points 属性值,item
是一个NSManagedObject
.
So basically I am having issues getting my head around adding numbers to properties of type NSNumber
. I am working with NSNumber
because they are properties of NSManagedObject
's.
所以基本上我在为 type 的属性添加数字时遇到了问题NSNumber
。我正在使用,NSNumber
因为它们是NSManagedObject
's 的属性。
Can anyone help me out ? I have over 80 errors which are all either one of the above errors mentioned.
谁能帮我吗 ?我有 80 多个错误,它们都是上述错误之一。
Thanks
谢谢
回答by Martin R
Before Swift 3, many types were automatically "bridged" to an
instance of some NSObject
subclass where necessary, such as String
to
NSString
, or Int
, Float
, ... to NSNumber
.
在 Swift 3 之前,许多类型会NSObject
在必要时自动“桥接”到某个子类的实例,例如String
to
NSString
, or Int
, Float
, ... to NSNumber
。
As of Swift 3 you have to make that conversion explicit:
从 Swift 3 开始,您必须明确转换:
var currentIndex = 0
for item in self.selectedFolder.arrayOfTasks {
item.index = currentIndex as NSNumber // <--
currentIndex += 1
}
Alternatively, use the option "Use scalar properties for primitive data types" when creating the NSManagedObject
subclass,
then the property has some integer type instead of NSNumber
,
so that you can get and set it without conversion.
或者,在创建NSManagedObject
子类时使用选项“为原始数据类型使用标量属性” ,然后该属性具有一些整数类型而不是NSNumber
,以便您无需转换即可获取和设置它。
回答by danieltmbr
In Swift 4 (and it might be the same in Swift 3) NSNumber(integer: Int)
was replaced with NSNumber(value: )
where value
can be any almost any type of number:
在 Swift 4 中(在 Swift 3 中可能相同)NSNumber(integer: Int)
被替换为NSNumber(value: )
wherevalue
几乎可以是任何类型的数字:
public init(value: Int8)
public init(value: UInt8)
public init(value: Int16)
public init(value: UInt16)
public init(value: Int32)
public init(value: UInt32)
public init(value: Int64)
public init(value: UInt64)
public init(value: Float)
public init(value: Double)
public init(value: Bool)
@available(iOS 2.0, *)
public init(value: Int)
@available(iOS 2.0, *)
public init(value: UInt)
回答by Hamed Ghadirian
Swift 4:
斯威夫特 4:
var currentIndex:Int = 0
for item in self.selectedFolder.arrayOfTasks {
item.index = NSNumber(value: currentIndex) // <--
currentIndex += 1
}
回答by jboi
You should stay with or original code and just change the assignment, so that it works:
您应该保留或原始代码并更改分配,以便它工作:
var currentIndex = 0
for item in self.selectedFolder.arrayOfTasks {
item.index = NSNumber(integer: currentIndex)
currentIndex += 1
}
As your code works fine in Swift 2, I'd expect that this is behaviour that might change in the next update.
由于您的代码在 Swift 2 中运行良好,我预计这种行为可能会在下一次更新中发生变化。
回答by FuatK
Swift 4.2
斯威夫特 4.2
item.index = Int(truncating: currentIndex)