[MacRuby-devel] Create new Core Data Instances

Timo Springmann timo at orangeorb.de
Sun Nov 13 14:03:41 PST 2011


A small step ahead...

... the following code seems to create an instance of MyEntity and sets the attribute 'attribute1' to the value 'Test'.

entityDesc = NSEntityDescription.new
entityDesc.setName("MyEntity")

att = NSAttributeDescription.new
att.setAttributeType(NSStringAttributeType)
att.setName("attribute1")
entityDesc.setProperties([att])

instance = MyEntity.alloc.initWithEntity(entityDesc, insertIntoManagedObjectContext:nil)
instance.setValue("Test", forKey:"attribute1")
 
Two question arising from this code:

a) Is it really THAT complicated to create an instance of MyEntity and set the attribute? I'm coming from the Rails/ActiveRecord world where this could easily be done with a single line of code (including saving the instantiated object): MyEntity.new(:attribute1 => "Test").save )

b) How can I save this fresh instance and update my NSTableView?

Regards,
Timo


Am 13.11.2011 um 22:20 schrieb Timo Springmann:

> Hi List,
> 
> I've successfully created a core data entity with some attributes. I'm displaying object in a NSTableView and I was able to add new instances by calling the entities array controller add: action.
> 
> The last hour I tried to figure out how to add/create a core data entity in ruby code without using the interface builder. I tried to instantiate a new object like this:
> 
> MyEntity.new
> 
> but I always get  the following error:
> 
> Failed to call designated initializer on NSManagedObject class 'MyEntity'
> 
> What's the proper way of doing this?
> 
> For rails-devs: I'm looking for something like:
> a = MyARObject.new
> a.attribute1 = "test"
> a.attribute2 = "test2
> a.save
> 
> Regards,
> Timo
> 
> -- 
> twitter.com/orangeorb
> 
> _______________________________________________
> MacRuby-devel mailing list
> MacRuby-devel at lists.macosforge.org
> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel

-- 
Ich im Web 2.0 - www.orangeorb.de - twitter.com/orangeorb



More information about the MacRuby-devel mailing list