Skip to content

Optional properties sometimes complain about NSNull (desired type = NSString; given type = NSNull) #22

Closed
@helje5

Description

@helje5

I'm sometimes getting this:

Unacceptable type of value for attribute: property = "xyz"; desired type = NSString; given type = NSNull; value = .

In this:

  func setValue<T>(forKey key: String, to value: T)
    where T: Codable & CoreDataPrimitiveValue & AnyOptional
  {
    willChangeValue(forKey: key); defer { didChangeValue(forKey: key) }
    let attr = Self.entity().attributesByName[key]
    print("Attr:", attr?.name, attr?.isOptional, attr?.attributeValueClassName)
    setPrimitiveValue(value, forKey: key)
  }

The property is optional, the valueClassName is set to NSString. Not quite sure what is going on here.

In CoreData those work:

    let item = Item(entity: Item.entity(), insertInto: nil)
    assert(item.entity === entity)
    
    item.value = nil
    item.setPrimitiveValue(nil, forKey: "value")

but this fails as well:

    item.setPrimitiveValue(NSNull(), forKey: "value")

Maybe a bridging issue w/ nil? As far as I can see, nil is properly used.

It fails in coerceValueForKey.

Metadata

Metadata

Assignees

Labels

bugSomething isn't workinghelp wantedExtra attention is needed

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions