0

I am trying to add a class object to an NSMutableArray but the object appears to be out of scope after adding it.

interface:

#import <Cocoa/Cocoa.h>

@class Person;

@interface MyDocument : NSDocument
 {
    NSMutableArray *employees;
    IBOutlet NSTableView *raiseTableView;
 }

- (IBAction)createEmployee:(id)sender;
- (IBAction)deleteSelectedEmployees:(id)sender;
@end

Part of the .m file:

#import "MyDocument.h"
#import "Person.h"
@implementation MyDocument


- (id)init
{
    self = [super init];
    if (self) {
        employees = [[[NSMutableArray alloc] init]retain];
    }
    return self;
}


- (IBAction)createEmployee:(id)sender
{
    Person *newEmployee = [[Person alloc] init];
    [employees addObject:newEmployee];
    NSLog(@"personName is: %@, expectedRaise is: %f", newEmployee.personName, newEmployee.expectedRaise);
    [newEmployee release];
    [raiseTableView reloadData];
}

The NSLog prints everything correctly. When I look at employees it shows 1 object added, when I look at the object it has a notation that it is out of scope and when I try to print it I get null for a result. Consequently, when it tries to reloadData things blow up. Anyone give me a hint as to what I am forgetting? Thanks.

TableView code:

#pragma mark Table view datasource methods

- (NSInteger)numberOfRowsInTableView:(NSTableView *)tempTableView
{
    return [employees count];
}

- (id)tableView:(NSTableView *)tempTableView objectValueForTableColumn:(NSTableColumn *)tempTableColumn row:(NSInteger)rowIndex
{
    // What is the identifier for the column?
    NSString *tempIdentifier = [tempTableColumn identifier];

    // What person?
    Person *tempPerson = [employees objectAtIndex:rowIndex];

    // What is the value of the attribute named identifier?
    return [tempPerson valueForKey:tempIdentifier];
}

- (void)tableView:(NSTableView *)tempTableView setObjectValue:(id)anObject forTableColumn:(NSTableColumn *)tempTableColumn row:(NSInteger)rowIndex
{
    NSString *tempIdentifier = [tempTableColumn identifier];
    Person *tempPerson = [employees objectAtIndex:rowIndex];

    // Set the value for the attribute named identifier
    [tempPerson setValue:anObject forKey:tempIdentifier];
}
8
  • I don't think the problem you're trying to solve (reloadData blowing up) is caused by this code. What error are you getting when things "blow up"? What does the stack trace point to? What does your TableViewDelegate look like? Commented Sep 18, 2011 at 17:42
  • Can you add the code where you use employees? Everything seems fine here. Anywhere else except the createEmployeemethod the newEmployee would be out of scope so I'm wondering where do you access it and get out of scope? Commented Sep 18, 2011 at 17:43
  • there might be some problem in cellForRowAtIndexPath as the code here seems to be OK at the moment. could you provide us with more code sample fro your project? Commented Sep 18, 2011 at 17:49
  • Unrelated to your problem, but you are over-retaining employees. You don't need to send retain; the fact that you have alloced it already means you own it. Commented Sep 18, 2011 at 17:52
  • @adpalumbo: This is the error message I get when I click the 'Create New Employee' button: 2011-09-20 09:32:50.484 RaiseMan[7548:707] [<Person 0x10044f700> valueForUndefinedKey:]: this class is not key value coding-compliant for the key (null). When I step through the code, the object is out of scope after it is added to the array and if I print the array it returns null. This is why I thought the error had to be in this part of the code. Not sure what you mean with what the TableViewDelegate looks like. I have done a litle work with delegates but do not fully understand them yet. Commented Sep 20, 2011 at 13:40

1 Answer 1

1

I think it's crashing because this line:

NSString *tempIdentifier = [tempTableColumn identifier];

is returning a null for tempIdentifier, so that the null string is getting passed to the Person class here:

NSString *tempIdentifier = [tempTableColumn identifier];

Which is causing the error message. You should print out the value of tempIdentifier to be sure.

Did you set the identity field for each column in your TableView in InterfaceBuilder?

Sign up to request clarification or add additional context in comments.

4 Comments

When I look at tempIdentifier it says it is nil. I thought I had set up the TableView links correctly unless I do not understand what you are referring to about the identity field.
I think you don't have the table set up correctly in InterfaceBuilder. Go into IB and select one of your columns in the table. Look at the Identity Inspector. Make sure you have the object of Class NSTableColumn. What's in the "Identifier" field? That will be what you get when you call [tempTableColumn identifier].
The 'Identifier' field has 'Automatic' in it. I do not know what it should be. I am going to be gone for a week so it will be next Sunday before I can respond to your answer but thanks for the help so far.
Now I am really confused. Tried to add some code to make it kvc compliant and nothing I tried helped so I removed the extra code and did a clean. When I tried to run the program again I got the message it could not find the file "Person.h" when I #import "Person.h" in the "My.Document.m" file. Where do I look for this error?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.