Projects

Ticket #507 (closed defect: fixed)

Opened 2 years ago

Last modified 22 months ago

valueForKey(key) no longer working with NSManagedObject subclasses.

Reported by: johnmacshea@… Owned by: lsansonetti@…
Priority: major Milestone: MacRuby 0.6
Component: MacRuby Keywords: valueForKey, KVC, Core Data
Cc:

Description

In MacRuby 0.4 - any method in an NSManagedObject subclass (or in any other class) could be bound (eg via a controller) to a (view) nscontroller/field.

Now - in 0.5 beta 1 and 2 (unless a variable of the same name is a property in the Entity that the NSManagedObject represents) valueForUndefinedKey(key) is called in the subclass, if that method exists, or an error occurs (if valueForUndefinedKey(key) does not exist) - eg: [<Student 0x200391280> valueForUndefinedKey:]: the entity Student is not key value coding-compliant for the key "age".

This does not occur in a non NSManagedObject subclass - when view objects are bound to other classes' methods(attributes) - it all works fine.

The different version of NSManagedObject valueForKey could be the culprit vis a vis valueForKey in NSObject, but in any case - as mentioned - it worked fine in 0.4.

Example project attached.

Attachments

CDKeyCheck.zip Download (54.3 KB) - added by johnmacshea@… 2 years ago.

Change History

Changed 2 years ago by johnmacshea@…

Changed 22 months ago by martinlagardette@…

Hi!

Could you please try with the latest trunk by downloading the nightly build? ( http://www.macruby.org/files/nightlies/)

I remember hitting the issue, but after trying to reduce the code which worked, I checked again with your test project, and didn't have any problem with this in Student.rb:

class Student < NSManagedObject
  attr_accessor :age
end

Changed 22 months ago by martinlagardette@…

Also if you ever want to try, here is the reduced code:

framework 'CoreData'

class Student < NSManagedObject
  attr_accessor :age

  def valueForUndefinedKey(key)
    p "#{self}::valueForUndefinedKey(#{key})"
  end

end

entityDesc = NSEntityDescription.new
entityDesc.setName("Student")
att = NSAttributeDescription.new
att.setAttributeType(NSStringAttributeType)
att.setName("age")
entityDesc.setProperties([att])
student = Student.alloc.initWithEntity(entityDesc, insertIntoManagedObjectContext:nil)
student.setValue("42", forKey:"age")
p student.valueForKey("age")

Changed 22 months ago by martinlagardette@…

Sorry, here is a real reduction that will correctly check:

framework 'CoreData'

class Student < NSManagedObject
  attr_accessor :age
end

if ARGV.count < 1
  puts "Usage: #{$0} <xcdatamodel file>"
  exit 1
end

# Compile the managed object model so we can open it
`/Developer/usr/bin/momc #{ARGV[0]} /tmp/MacRubyCDKeyCheck.mom`
model = NSURL.URLWithString("file:///tmp/MacRubyCDKeyCheck.mom")
mom = NSManagedObjectModel.alloc.initWithContentsOfURL(model)
s = Student.alloc.initWithEntity(mom.entitiesByName["Student"], insertIntoManagedObjectContext:nil)
s.valueForKey("age")

This works by doing so:

$> macruby kvc.rb ~/Downloads/CDKeyCheck/MacRubyApp.xcdatamodel
NSUnknownKeyException: [<Student 0x40073e9a0> valueForUndefinedKey:]: the entity Student is not key value coding-compliant for the key "age". (RuntimeError)
$>

On trunk:

$> macruby kvc.rb ~/Downloads/CDKeyCheck/MacRubyApp.xcdatamodel
nil
$>

Changed 22 months ago by martinlagardette@…

  • status changed from new to closed
  • resolution set to fixed

btw, closing this as it is fixed

Changed 22 months ago by lsansonetti@…

  • milestone set to MacRuby 0.6
Note: See TracTickets for help on using tickets.