Fundamental delegate question

Discussion in 'Mac Programming' started by ace2600, Mar 30, 2008.

  1. macrumors member

    I know this is a basic delegate concept, and I should probably know the answer, but I do not.

    I have a class as follows:
    @interface TableController UIViewController <UITableViewDelegateUITableViewDataSource> {
    UITableView    *myTableView;
    //Array of strings
    NSArray *arr;
    - (void)loadView {
    myTableView.delegate self;
    myTableView.dataSource self;
    //Method for UITableViewDelegate
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath withAvailableCell:(UITableViewCell *)availableCell {
    //Want to access arr here
    if([arr count]) {
    Is there a way to access arr? I realize it belongs to TableController, but the delegate myTableView.delegate is set to self. If I cannot access arr directly, can I access indirectly (like through the invoker?) or somehow pass it to the delegate?
  2. Moderator emeritus


    You can access arr directly. It is an instance variable of the TableController class.

    You are assigning the delegate to self. That means self must respond to certain methods. These methods are just normal methods in the class. Any method in the class can access ivars directly.
  3. macrumors member

    Thanks Kainjow.

    Okay, the problem appears to be when I access arr from the delegate methods (like cellForRowAtIndexPath), it gives me this error:
    3/31/08 10:09:35 AM HomeFinder[1523] *** -[_UITableViewReorderingSupport count]: unrecognized selector sent to instance 0x355d20 
    3/31/08 10:09:35 AM HomeFinder[1523] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[_UITableViewReorderingSupport count]: unrecognized selector sent to instance 0x355d20' 
    I can access arr just fine with the UITableViewDataSource methods like numberOfRowsInSection.

    I have a feeling it's referencing the count for another variable (_UITableViewReorderingSupport) instead of arr, but not sure why.
  4. Moderator emeritus


    Sounds like a memory issue. Can you post the other code where you're creating arr?
  5. macrumors member

    Thanks again Kainjow.

    When initializing it with the convenience constructor arrayWithObjects, it fails, but using initWithObjects, it works.
    - (id)init
        if (self = [super init])
            // this title will appear in the navigation bar
            self.title = @"Title";
        //Causes error described earlier
        //arr = [NSArray arrayWithObjects:@"first string", @"second string", nil];
        arr = [[NSArray alloc] initWithObjects:@"test1", @"test2", nil];
        return self;
    I'm rereading the memory management articles on apple, but still not sure why arr does not seem to be accessible when using arrayWithObjects.

    Also, arr is defined in the header file exactly as described in the original post, NSArray *arr. No @property.
  6. Moderator emeritus


    arrayWithObjects returns an autoreleased object, whereas alloc/init does not. Autoreleased objects get released at another point in time and do not hang around.

    So if you change your code to this it should work:

    arr = [[NSArray arrayWithObjects:@"first string", @"second string", nil] retain];
    Just make sure you release arr in your dealloc method.
  7. macrumors member

    Thanks Kainjow!

    Is there any difference/preference between:
    arr = [[NSArray arrayWithObjects:@"first string", @"second string", nil] retain];
    arr = [[NSArray alloc] initWithObjects:@"first string", @"second string", nil];
    if I'm releasing both in dealloc?

    And one last question on memory management.
    if I had this:
    NSArray *subarr = [[NSArray arrayWithObjects:@"first string", @"second string", nil]
    arr = [[NSArray arrayWithObjects:subarr, nil] retain];
    Would subarr be released when I release arr?
  8. Moderator emeritus


    The only difference is with arrayWithObjects, you're retaining an autoreleased object, which kind of defeats the purpose (since you have the option of using one or the other). Think of it as like this:

    arr = [[[[NSArray alloc] initWithObjects:@"first string", @"second string", nil] autorelease] retain];
    Yes. arr will retain subarr, and so when arr is released it releases all of its objects. If you retain subarr, before or after adding it to arr, you will need to release it yourself at some point.

Share This Page