Hey everyone
I was hoping someone here could help me. I'm quite new to both Obj-c and Cocoa and have been trying to teach myself by building a few trivial apps. The latest is a searchable dictionary app, which allows users to add and remove entries. (I want to use it for conlangs)
Anyway the app is a Cocoa document based app and has three classes.
Now I thought that in order to add saving to the app I had to implement a few methods in the MyDocument class and have custom classes to archive themselves.
So in MyDocument I added this:
Where words is an NSMuatableArray. The class also has an IBOutlet to an NSArrayController which is linked to my custom NSArrayController. All the UI elements are bound to this ArrayController.
In my Word class I've added the following:
From what I undertsand this should have worked. The app seems to save fine, but When i load up a file the UI elements are not populated. Is this because I need to manually repopulate the ArrayController or is there something really simple I'm missing here.?
I was hoping someone here could help me. I'm quite new to both Obj-c and Cocoa and have been trying to teach myself by building a few trivial apps. The latest is a searchable dictionary app, which allows users to add and remove entries. (I want to use it for conlangs)
Anyway the app is a Cocoa document based app and has three classes.
- a MyDocument class which inherits from NSDocument
- a FilteredArrayController which inherits from NSArrayController. It reimplements the newObject: and adds a search functionailty by reimplementing arrangeObjects:
- a Word class which stores a word, it's meaning, etymology and notes.
Now I thought that in order to add saving to the app I had to implement a few methods in the MyDocument class and have custom classes to archive themselves.
So in MyDocument I added this:
- (void)windowControllerDidLoadNib: (NSWindowController *) aController
{
[super windowControllerDidLoadNib:aController];
}
- (NSData *)dataRepresentationOfType: (NSString *)aType
{
return [NSKeyedArchiver archivedDataWithRootObject:words];
}
- (BOOL)loadDataRepresentation: (NSData *)data ofType: (NSString *)aType
{
[self setWords:[NSKeyedUnarchiver unarchiveObjectWithData:data]];
return YES;
}
Where words is an NSMuatableArray. The class also has an IBOutlet to an NSArrayController which is linked to my custom NSArrayController. All the UI elements are bound to this ArrayController.
In my Word class I've added the following:
- (void)encodeWithCoder: (NSCoder *)coder
{
[coder encodeObject: _word forKey"word"];
[coder encodeObject: _meaning forKey"meaning"];
[coder encodeObject: _notes forKey"notes"];
[coder encodeObject: _etym forKey"etym"];
}
-(id)initWithCoder: (NSCoder*)coder
{
if (self = [super init])
{
[self setWord: [coder decodeObjectForKey"word"]];
[self setMeaning:[coder decodeObjectForKey"meaning"]];
[self setNotes:[coder decodeObjectForKey"notes"]];
[self setEtym: [coder decodeObjectForKey"eytm"]];
}
}
From what I undertsand this should have worked. The app seems to save fine, but When i load up a file the UI elements are not populated. Is this because I need to manually repopulate the ArrayController or is there something really simple I'm missing here.?