Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 与NSNumber&;Swift 3中的整数值_Ios_Swift_Int_Swift3_Nsnumber - Fatal编程技术网

Ios 与NSNumber&;Swift 3中的整数值

Ios 与NSNumber&;Swift 3中的整数值,ios,swift,int,swift3,nsnumber,Ios,Swift,Int,Swift3,Nsnumber,我正在尝试将我的项目转换为Swift 3.0,但是在使用NSNumber和整数时,我收到两条错误消息 无法将类型int分配给类型NSNumber 为了 即使我将currentIndex更改为类型NSNumber,也会得到错误 二进制运算符“+=”不能应用于类型“NSNumber”和“Int” 然后我创建了一个名为one的属性,类型为NSNumber,添加到currentIndex,但随后得到以下错误: 二进制运算符“+=”不能应用于两个NSNumber操作数 &&我得到的第二个错误是 没有“+”

我正在尝试将我的项目转换为Swift 3.0,但是在使用
NSNumber
整数时,我收到两条错误消息

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

为了

即使我将
currentIndex
更改为类型
NSNumber
,也会得到错误

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

然后我创建了一个名为
one
的属性,类型为
NSNumber
,添加到
currentIndex
,但随后得到以下错误:

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

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

没有“+”候选项生成预期的上下文结果类型NSNumber

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

因此,基本上,我在为
NSNumber
类型的属性添加数字时遇到了一些问题。我使用的是
NSNumber
,因为它们是
NSManagedObject
的属性

有人能帮我吗?我有80多个错误,都是上面提到的其中一个错误


谢谢

您应该继续使用或使用原始代码,只需更改作业即可:

var currentIndex = 0
for item in self.selectedFolder.arrayOfTasks {
    item.index = NSNumber(integer: currentIndex)
    currentIndex += 1
}

由于您的代码在Swift 2中运行良好,我希望这是下一次更新中可能会改变的行为。

在Swift 3之前,许多类型自动“桥接”到 某些
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
}
var currentIndex=0
对于self.selectedFolder.arrayOfTasks中的项目{
item.index=currentIndex在Swift 4中作为NSNumber/(在Swift 3中可能相同)
NSNumber(整数:Int)
被替换为
NSNumber(值:)
其中
value
可以是几乎任何类型的数字:

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)

Swift 4

var currentIndex:Int = 0
for item in self.selectedFolder.arrayOfTasks {
   item.index = NSNumber(value: currentIndex) // <--
   currentIndex += 1
}
var currentIndex:Int=0
对于self.selectedFolder.arrayOfTasks中的项目{
item.index=NSNumber(值:currentIndex)/Swift 4.2

item.index = Int(truncating: currentIndex)

您可以在这里找到完整的解释:
var currentIndex:Int = 0
for item in self.selectedFolder.arrayOfTasks {
   item.index = NSNumber(value: currentIndex) // <--
   currentIndex += 1
}
item.index = Int(truncating: currentIndex)