1

I am newbie with Cocoa Touch, I have a problem that I try to figure out. I will appreciate if anyone could help.

I would like to create a tableDataList to display on the table. AsyncViewController will call TableHandler fillList method to initialize table data. But after fillList call, the tableDataList return empty.

Can anyone explain me this problem?

In "AsyncViewController.m":

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [self.myHandler fillList];
    [super viewDidLoad];
}

In "TableHandler.m":

@implementation TableHandler

#define ItemNumber 20

@synthesize tableDataList;

- (void) fillList {
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:(NSUInteger) 20];

    for (NSUInteger i = 0; i < ItemNumber; i++) {
        [array addObject:[NSString stringWithFormat:@"Row %d", i]];
    }

    tableDataList = [NSArray arrayWithArray:array];
}

Thanks

3
  • Hi Stephen, how I can format my question like you did for me? Thanks Commented Aug 10, 2010 at 9:26
  • I think you can hit 'edit' to see how he formatted it. Commented Aug 10, 2010 at 10:12
  • If not, there's a bright orange "?" box beside the editing field which you can click on to get all sorts of useful info. It goes here: stackoverflow.com/editing-help Commented Aug 10, 2010 at 10:13

2 Answers 2

1

tableDataList needs to retain the new array, or it will be autoreleased soon after your call.

If tableDataList is a @property with retain, just change the line above to:

self.tableDataList = [NSArray arrayWithArray:array];

and the setter will handle it for you.

The equivalent of a @property (retain) NSArray *tableDataList; is in code,

- (void)setTableDataList:(NSArray *)anArray
{
    if (tableDataList != nil) {
        [tableDataList autorelease]; 
    }
    tableDataList = [anArray retain];
}

The above code will automatically release and retain objects when you replace the variable, using self.tableDataList = SOMETHING. However, if you just use tableDataList = SOMETHING you are not using the above setter, you're setting the variable directly.

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

2 Comments

Oh, that's works. Would you please explain me the difference between tableDataList and self.tableDataList? Sorry about this stupid question. Many thanks
The difference is that when you use "self.variable" you are calling the setter for the variable. The setter is a convenience method that you have set up using @property and @synthesize. Since you set it to @property (retain) in your header file, the system will do something like ... (see answer above, I've updated it).
0

Are you sure it's empty and not nil? You may need to alloc the tableDataList before you use it

2 Comments

I add [tableDataList count] to Expressions Window and I receive "out of scope", after that the program received signal: "EXC_BAD_ACCESS" I think the NSMutableArray *array is empty when go out of function. So the tableDataList is empty. Is that right?
It wouldn't be nil, it would point to a garbage location in memory, where the released array used to be.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.