TahDoodle doesn't save


#1

My Tahdoodle does not save to do items. The code worked fine up to p203, where I can add a new item. But once I close and reopen the window, the added to do list has disappeared. There is no warning or error message. I am pretty sure I have typed the code in as it is in the book. So the code I added in from p204 doesn’t do anything. Have I missed something here?

I have made the three connections: tableView – itemTableView, tableView – dataSource, button – createNewItem.
I have upgraded to lion and install Xcode4.2.1 and made sure I have turned on the ARC from the build setting.

Please help.


#import <Cocoa/Cocoa.h>

@interface BNRDocument : NSDocument <NSTableViewDataSource>
{
    
    NSMutableArray *todoItems;
    IBOutlet NSTableView *itemTableView;

}

-(IBAction)createNewItem:(id)sender;


@end
#import "BNRDocument.h"

@implementation BNRDocument

#pragma mark  - NSDocument Overrides
-(NSString *)windowNibName
{
    return @"BNRDocument";
}


#pragma mark - Actions
-(IBAction)createNewItem:(id)sender
{
    // If there is no array yet, go ahead and create oneto store our new task.
    if (!todoItems) {
        todoItems = [NSMutableArray array];
    }
    
   // [todoItems retain];
    
    [todoItems addObject:@"New Item"];
    
    // - reloadData: tells teh table view to refresh and ask its dataSource (which happens to be this BNRDoucument object in this case).
    [itemTableView reloadData];
    
    // - (updateChangeCount: tells the application whether or not the document has unsaved changes.NSChangeDone fages the document as unsaved.
    [self updateChangeCount:NSChangeDone];
}


#pragma mark - Data Source Methods

-(NSInteger) numberOfRowsInTableView:(NSTableView *)tableView
{
    // This table view is meant to display the todoItems, so the number of entries in the table view will be the same as the number of objects in the array.
    return [todoItems count];
}


-(id) tableView:(NSTableView *)tableView 
    objectValueForTableColumn:(NSTableColumn *)tableColumn 
                          row:(NSInteger)row
{
    // Return the item from todoItems that corresponds to the cell that the table view wants to display   
    return [todoItems objectAtIndex:row];
}


-(void) tableView:(NSTableView *)tableView 
   setObjectValue:(id)object 
   forTableColumn:(NSTableColumn *)tableColumn 
              row:(NSInteger)row
{
    // When the user change a to-do item on the table view. update the todoItems array.
    [todoItems replaceObjectAtIndex:row withObject:object];
    // And flag the document as having unsaved changes.
    [self updateChangeCount:NSChangeDone];
}


-(NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError
{
    // This method is called when our document is being saved.
    // We ar expected to hand the caller an NSData object wrapping our data so that it ican be written to disck.
    
    
    // If there's no array, we'll write our an empty arry for now
    if (!todoItems) {
        todoItems = [NSMutableArray array];
    }
    
    // Pack our todoItems array into an NSData object
    NSData *data = [NSPropertyListSerialization
                    dataWithPropertyList:todoItems
                    format:NSPropertyListXMLFormat_v1_0
                    options:NSPropertyListMutableContainers
                    error];
    
    
    // Return our newly-packed NSData object
    return data;
    
}


-(BOOL) readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError
{
    // This method is called when a document is being loaded.
    // We are handed an NSData object and expected to pull our data out of it.
    
    // Extract our todoItems
    todoItems = [NSPropertyListSerialization propertyListWithData:data 
                                                          options:0 
                                                           format:NULL
                                                            error];
    
    return (todoItems != nil);
}



- (id)init
{
    self = [super init];
    if (self) {
        // Add your subclass-specific initialization here.
        // If an error occurs here, return nil.
    }
    return self;
}

- (void)windowControllerDidLoadNib:(NSWindowController *)aController
{
    [super windowControllerDidLoadNib:aController];
    // Add any code here that needs to be executed once the windowController has loaded the document's window.
}

@end

#2

Do you really saved the list on your hard drive or only typed in some items and closed the application? You must save the list with command+s to your hard drive. Your code seems to be fine.


#3

I can’t save my TahDoodle either, every time I try I get this (I tried to paste a screen capture but for some reason it’s not working): "The document ‘untitled’ could not be saved as ‘Untitled.tdl’. You don’t have permission. To view or change permissions, select the item in the Finder and choose File > Get Info."
How can I select an item in finder if I can’t save it in the first place?


#4

I see what the problem is…When you have your app window selected and running, look up at the top most control bar of your computer screen, you’ll see TahDoodle and menu items like: file, edit, format, view, window, and help.

Explore these menu items and all will become clear.

and yes command-s does bring up the “save as” pop up window.