Working with NSNumber & Integer values in Swift 3
Asked Answered
L

5

46

I am trying to convert my project to Swift 3.0 however I am having two error messages when working with NSNumber and Integers.

Cannot assign type int to type 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

Binary operator '+=' cannot be applied to type 'NSNumber' and 'Int'

so then I create a property called one of type NSNumber to add to currentIndex but then get the following error;

Binary operator '+=' cannot be applied to two NSNumber operands

&& the second error I get is

No '+' candidates produce the expected contextual result type 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.

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.

Can anyone help me out ? I have over 80 errors which are all either one of the above errors mentioned.

Thanks

Legate answered 4/9, 2016 at 20:55 Comment(0)
A
74

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.

As of Swift 3 you have to make that conversion explicit:

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.

Accidental answered 4/9, 2016 at 21:12 Comment(1)
You can find the full explanation here : SE-0072Octa
P
18

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:

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)
Parthenogenesis answered 22/10, 2017 at 9:53 Comment(0)
P
7

Swift 4:

var currentIndex:Int = 0
for item in self.selectedFolder.arrayOfTasks {
   item.index = NSNumber(value: currentIndex) // <--
   currentIndex += 1
}
Pharisee answered 22/11, 2017 at 11:4 Comment(0)
I
3

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.

Insectile answered 4/9, 2016 at 21:10 Comment(0)
D
2

Swift 4.2

item.index = Int(truncating: currentIndex)
Disenfranchise answered 1/4, 2019 at 13:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.