2

I am testing NSMutableArray and do not understand what i am doing wrong.

When i define "myArray" locally the [myArray addObject:@"First line"]; works but if i define it in the .h file it ends up as null.

I am using myArray to add the text of multiple selected cell's in a UITableView "didSelectRowAtIndexPath".

Could someone nice explain what i am doing wrong?

1
  • 1
    The problem is in your code - you'll need to paste in the declaration and initialization snippits for someone to help you. Commented Nov 26, 2010 at 22:23

3 Answers 3

2

Make sure you have it like this in your .h file:

@interface YourViewController : UIViewController {

    NSMutableArray *myArray;
}

@property(nonatomic, retain) NSMutableArray *myArray;

@end

And in your .m file

@implementation YourViewController


@synthesize myArray

//make sure you release it
- (void)dealloc {
    [myArray release];
}

/*....other code..... */

/*make sure it's initialised somewhere */
- (void)viewDidLoad {
    [super viewDidLoad];
    myArray = [[NSMutableArray] alloc] init];
}   
@end
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks gef, this is exactly how i did it :-)
1

Are you just declaring an NSMutableArray in the header file? If so, it still needs to be initialized somewhere in the .m file. You could do this like so:

array = [[NSMutableArray alloc] init];
[array addObject:@"First line"];

1 Comment

Thanks, that did it. BTW, i guess I should then release it when i leave the UITableView xib?
0

In .h file

NSMutableArray *myArray;

In .m file

-(void)viewDidLoad
{
[super viewDidLoad];
myArray = [[NSMutableArray] alloc] init];
[myArray addObject:@"myObject"];
}

Comments

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.