0

I have a Core Data entity set up with the following attributes:

resellerNo:Int
resellerName:String

I have setup an NSManagedObject as follows:

class Reseller: NSManagedObject
{
    @NSManaged var resellerNo: Int
    @NSManaged var resellerName: String
}

If I try to run this method:

func createNewReseller(resellerName: String)
{
    let context = app.managedObjectContext

    let resellerEntity = NSEntityDescription.entityForName("Resellers", inManagedObjectContext: context)
    let newReseller = Reseller(entity: resellerEntity!, insertIntoManagedObjectContext: context)

    newReseller.resellerNo = 12
    newReseller.resellerName = resellerName
    saveDatabase()
    Swift.print ("Reseller \(resellerName) created")
}

then it crashes when trying to allocate the resellerNo with an error message:

Unacceptable type of value for attribute: property = "resellerNo"; desired type = NSNumber; given type = __NSTaggedDate; value = 2001-01-01 00:00:00 +0000.

Strange thing is, if you use the console to print newReseller.resellerNo just beforehand then it works fine.

Other code accessing other Entities in exactly the same way work fine.

Any ideas?

Vivek Molkar
  • 3,910
  • 1
  • 34
  • 46
iphaaw
  • 6,764
  • 11
  • 58
  • 83
  • not sure this will answer, can you change `newReseller.resellerNo = 12` to use `newReseller.resellerNo = NSNumber(int: 12)` and see if the error goes? – Vivek Molkar Apr 03 '16 at 15:28
  • No Xcode didn't like this. Cannot assign a value of type 'NSNumber' to a value of type 'int'. – iphaaw Apr 03 '16 at 15:34

3 Answers3

0

OK it turned out to be fairly simple in the end. It turns out I had not added a class to the Entity.

If your having this problem:

  1. Click on the xcdatamodel
  2. Choose the entity.
  3. Show the Data Model Inspector in the Utilities bar.
  4. Enter the class name defined (in my case Reseller)

I also had to change my class definition to this:

@objc(Reseller)
class Reseller: NSManagedObject
{
    @NSManaged var resellerNo: Int
    @NSManaged var resellerName: String
}

Hope this helps someone.

iphaaw
  • 6,764
  • 11
  • 58
  • 83
0

I had this same error message in a table view binding. The cell (text field) defaults to returned value of string but Core Data wanted it as an NSNumber, hence seeing the unacceptable type value error.

By simply dragging a number formatter onto the table cell view it forced the interpreted type of the cell to be a number and the error went away.

obj-D
  • 210
  • 1
  • 4
0

Old post but just wanted to add that I had this same issue and mine was similar to iphaaw's answer but with mine I was setting a parent entity for the core data class and in the xcdatamodel - I forgot to set the parent entity here. So my steps were

(Already had the Core Data Class setting the parent entity in code)

Same first 3 steps as iphaaw - Go to xcdatamodel, choose your entity, show the data model inspector on the right side

  1. Click the Parent Entity drop down and fill in what should be there
Palmer
  • 26
  • 4