From: Allison N. <dem...@ma...> - 2008-07-26 08:16:20
|
Stephyn, You have to be very careful using YAML with NSObjects - YAML doesn't know how to save the NSObject part of the object, or at least, it doesn't know how to reinitialise the object correctly when you do a YAML.load. When I ran into a similar problem (I was trying to use YAML data with an NSOutlineView), I eventually solved it by implementing the NSCoding protocol. The code looks like this: def encodeWithCoder(coder) coder.encodeObject_forKey(@exercise_machines, "exercise_machines") coder.encodeObject_forKey(@programs, "programs") coder.encodeObject_forKey(@schedules, "schedules") end def initWithCoder(coder) init @exercise_machines = coder.decodeObjectForKey("exercise_machines") @programs = coder.decodeObjectForKey("programs") @schedules = coder.decodeObjectForKey("schedules") self end def save NSLog("Saving archive") result = NSKeyedArchiver.archiveRootObject_toFile_(self, FILE_NAME) end def load lib = NSKeyedUnarchiver.unarchiveObjectWithFile(FILE_NAME) end Otherwise you get all sorts of bad things happening - objects that blow up when loading, objects that die when the NSTable tries to call NSObject methods on your YAML-loaded objects etc etc. Bad things. Hope that helps. Alli Le 26 juil. 08 à 08:06, Stephyn Butcher a écrit : > Hi all, > > I have a very simple app I'm trying to write to spike out some key > portions of an app I'm planning to write. > > The application is not document based but I do want to be able to > save and load the object model. The "preferred" form is YAML with > import/export using XML. The application is dirt simple. Just an > array of Friends visible through a NSTable and the Friend objects > themselves which can be edited. > > I can successfully save the @friends array as a YAML file > (saveFriends)...but I get thrown into the debugger every time I > tried to load a YAML file (openFriends). I've narrowed it down to > YAML::load as the line that crashes (I can read the file, and print > out the contents to NSLog just fine). > > Any ideas? > > If you're curious and want to kick the tires, you can download the > project at http://homepage.mac.com/stephyn/Friends.zip > > Thanks a bunch. > > Cheers, > Steve > > > ---Friend.rb > require 'osx/cocoa' > > class Friend < OSX::NSObject > include OSX > > kvc_accessor :name, :email, :cell > > def init > if super_init > @name = "New Friend" > @email = "New Email" > @cell = "New Cell" > self > end > end > end > > ---AppController.rb > require 'osx/cocoa' > require 'yaml' > > class AppController < OSX::NSObject > include OSX > > kvc_accessor :friends > > def init > if super_init > @friends = NSMutableArray.array > self > end > end > > def saveFriends( sender) > friends = @friends.to_ruby > > panel = NSSavePanel.savePanel > if panel.runModal == NSFileHandlingPanelOKButton > filename = panel.filename.to_ruby > else > return > end > File.open( filename, "w") { |file| file.puts friends.to_yaml} > end > ib_action :saveFriends > > def openFriends( sender) > panel = NSOpenPanel.openPanel > if panel.runModal == NSFileHandlingPanelOKButton > filename = panel.filename.to_ruby > else > return > end > @friends = YAML::load( File.open( filename, "r")) > end > ib_action :openFriends > end > > ------------------------------------------------------------------------- > This SF.Net email is sponsored by the Moblin Your Move Developer's > challenge > Build the coolest Linux based applications with Moblin SDK & win > great prizes > Grand prize is a trip for two to an Open Source event anywhere in > the world > http://moblin-contest.org/redirect.php?banner_id=100&url=/_______________________________________________ > Rubycocoa-talk mailing list > Rub...@li... > https://lists.sourceforge.net/lists/listinfo/rubycocoa-talk |