Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jun 25, 2007, 06:19 AM   #1
zedLondon
macrumors member
 
Join Date: Jun 2007
Location: London
Adding saving to a Document based app

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.
  1. a MyDocument class which inherits from NSDocument
  2. a FilteredArrayController which inherits from NSArrayController. It reimplements the newObject: and adds a search functionailty by reimplementing arrangeObjects:
  3. 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:
Quote:
- (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:

Quote:
- (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.?
zedLondon is offline   0 Reply With Quote
Old Jun 25, 2007, 06:29 AM   #2
cbougher
macrumors member
 
Join Date: Oct 2006
Location: Atlanta, GA
Does your Word clas implement the NSCoding protocol?
cbougher is offline   0 Reply With Quote
Old Jun 25, 2007, 06:50 AM   #3
zedLondon
Thread Starter
macrumors member
 
Join Date: Jun 2007
Location: London
Quote:
Originally Posted by cbougher View Post
Does your Word clas implement the NSCoding protocol?
Yes it does

If i understand it correctly all I need to do is this:

Quote:
@interface word : NSObject <NSCoding>
to implement NSCoding right?
zedLondon is offline   0 Reply With Quote
Old Jun 25, 2007, 02:21 PM   #4
zedLondon
Thread Starter
macrumors member
 
Join Date: Jun 2007
Location: London
sussed it

I deserve a great big thwak over the head. I forgot to set the content array binding of my array controller. So when my document attempted to archive my words array, it was empty as the array controller hadn't been passing the info back to it.

*slaps forehead*
zedLondon is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
basic core data saving in a document-based app Yashman! Mac Programming 0 Apr 10, 2014 07:58 AM
Core Data app w/out Document based app cpuin Mac Programming 2 Jun 17, 2013 02:14 PM
Any way to close pages document without saving? jbachandouris Mac Applications and Mac App Store 1 Dec 13, 2012 12:21 PM

Forum Jump

All times are GMT -5. The time now is 04:08 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC