Uninitialized mutable array object


#1

Hi,

I’m currently trying to get this first app to run.

Here is my code for BKDDocument.h :

[code]//
// BKDDocument.h
// Taches
//
// Created by blackndoor on 27/02/2014.
// Copyright © 2014 gj. All rights reserved.
//

#import <Cocoa/Cocoa.h>

@interface BKDDocument : NSDocument
{
NSMutableArray *tachesAFaire;
IBOutlet NSTableView *tachesTableView;
}

-(IBAction)creerNouvelleTache:(id)sender;

@end[/code]

and for BKDDocument.m :

[code]//
// BKDDocument.m
// Taches
//
// Created by blackndoor on 27/02/2014.
// Copyright © 2014 gj. All rights reserved.
//

#import “BKDDocument.h”

@implementation BKDDocument

  • (id)init
    {
    self = [super init];
    if (self) {
    // Add your subclass-specific initialization here.

    }
    return self;
    }

#pragma mark - Redéfinition pour NSDocument

  • (NSString *)windowNibName
    {
    // Override returning the nib file name of the document
    // If you need to use a subclass of NSWindowController or if your document supports multiple NSWindowControllers, you should remove this method and override -makeWindowControllers instead.
    return @“BKDDocument”;
    }

#pragma mark - Actions

-(IBAction)creerNouvelleTache:(id)sender
{

if(!tachesAFaire)
    tachesAFaire = [NSMutableArray alloc];

[tachesAFaire addObject:@"Nouvelle tache"];

[tachesTableView reloadData];

[self updateChangeCount:NSChangeDone];

}

#pragma mark - Méthodes de la source de données NSTableViewDataSource

-(NSInteger)numberOfRowsInTableView:(NSTableView *)tableView
{

return [tachesAFaire count];

}

-(id)tableView: (NSTableView *) tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{

return [tachesAFaire objectAtIndex:row];

}

-(void)tableView: (NSTableView *)tableView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{

[tachesAFaire replaceObjectAtIndex:row withObject:object];

[self updateChangeCount:NSChangeDone];

}

  • (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.
    }
  • (BOOL)autosavesInPlace
    {
    return YES;
    }
  • (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError
    {
    // Insert code here to write your document to data of the specified type. If outError != NULL, ensure that you create and set an appropriate error when returning nil.
    // You can also choose to override -fileWrapperOfType:error:, -writeToURL:ofType:error:, or -writeToURL:ofType:forSaveOperation:originalContentsURL:error: instead.
    NSException *exception = [NSException exceptionWithName:@“UnimplementedMethod” reason:[NSString stringWithFormat:@"%@ is unimplemented", NSStringFromSelector(_cmd)] userInfo:nil];
    @throw exception;
    return nil;
    }

  • (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError
    {
    // Insert code here to read your document from the given data of the specified type. If outError != NULL, ensure that you create and set an appropriate error when returning NO.
    // You can also choose to override -readFromFileWrapper:ofType:error: or -readFromURL:ofType:error: instead.
    // If you override either of these, you should also override -isEntireFileLoaded to return NO if the contents are lazily loaded.
    NSException *exception = [NSException exceptionWithName:@“UnimplementedMethod” reason:[NSString stringWithFormat:@"%@ is unimplemented", NSStringFromSelector(_cmd)] userInfo:nil];
    @throw exception;
    return YES;
    }

@end[/code]

I’m able to run the app but when i click on the button Insert, nothing happens in the app.
This is what I get on Debug :

2014-02-27 17:11:13.527 Taches[2382:303] *** -[NSMutableArray count]: method sent to an uninitialized mutable array object 2014-02-27 17:11:13.529 Taches[2382:303] ( 0 CoreFoundation 0x00007fff87b0841c __exceptionPreprocess + 172 1 libobjc.A.dylib 0x00007fff8b355e75 objc_exception_throw + 43 2 CoreFoundation 0x00007fff87af81dd -[__NSPlaceholderArray count] + 189 3 CoreFoundation 0x00007fff87afb46f -[NSMutableArray addObject:] + 47 4 Taches 0x0000000100000fb0 -[BKDDocument creerNouvelleTache:] + 176 5 AppKit 0x00007fff8cc2d3d0 -[NSApplication sendAction:to:from:] + 327 6 AppKit 0x00007fff8cc2d24e -[NSControl sendAction:to:] + 86 7 AppKit 0x00007fff8cc79d7d -[NSCell _sendActionFrom:] + 128 8 AppKit 0x00007fff8cc93715 -[NSCell trackMouse:inRect:ofView:untilMouseUp:] + 2316 9 AppKit 0x00007fff8cc92ae7 -[NSButtonCell trackMouse:inRect:ofView:untilMouseUp:] + 487 10 AppKit 0x00007fff8cc921fd -[NSControl mouseDown:] + 706 11 AppKit 0x00007fff8cc13d08 -[NSWindow sendEvent:] + 11296 12 AppKit 0x00007fff8cbb2744 -[NSApplication sendEvent:] + 2021 13 AppKit 0x00007fff8ca02a29 -[NSApplication run] + 646 14 AppKit 0x00007fff8c9ed803 NSApplicationMain + 940 15 Taches 0x0000000100001592 main + 34 16 libdyld.dylib 0x00007fff8dc275fd start + 1 )

It’s true, the NSMUtableArray object is not initialized, as you can read bellow :

-(IBAction)creerNouvelleTache:(id)sender
{
    
    if(!tachesAFaire)
       => tachesAFaire = [NSMutableArray alloc]; <=
    
    [tachesAFaire addObject:@"Nouvelle tache"];
    
    [tachesTableView reloadData];
    
    [self updateChangeCount:NSChangeDone];
    
}

I tried to fixe this issue and changed the line by :

[code]-(IBAction)creerNouvelleTache:(id)sender
{

if(!tachesAFaire)
  =>  tachesAFaire = [[NSMutableArray alloc] init]; <=

[tachesAFaire addObject:@"Nouvelle tache"];[/code]

It works. I can click on insert and add a new entry but I would like to know if it is an error in the initial code (from the book) or if something is not configured correctly on my mac.

Thanks in advance for your help


#2

Hi,

Feel stupid because I realized that I read 10 times the code without seeing that I made a mistake…
The variable was initialized by the method array and not alloc…

Sorry for the useless post.