Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding NSInteger object to NSMutableArray

I have an NSMutableArray

@interface DetailViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {

NSMutableArray *reponses;
}
@property (nonatomic, retain) NSMutableArray *reponses;

@end

and i'm trying to add in my array NSInteger object:

@synthesize reponses;



NSInteger val2 = [indexPath row];
[reponses addObject:[NSNumber numberWithInteger:val2]];
NSLog(@"the array is %@ and the value is %i",reponses, val2);

it won't work the object was not added to the array, this is what console shows:

the array is (null) and the value is 2
like image 486
Hamdi-Lachaal Avatar asked Dec 22 '11 07:12

Hamdi-Lachaal


2 Answers

@Omz: is right. Make sure you have the array allocated and initialized. Please check the following code

NSMutableArray *array = [[NSMutableArray alloc] init];
NSInteger num = 7;
NSNumber *number = [NSNumber numberWithInt:num];
[ar addObject:number];
NSLog(@"Array %@",array);

I have checked this and it works. If array is no longer needed make sure you release it.

like image 55
visakh7 Avatar answered Sep 20 '22 12:09

visakh7


You're not initializing your array, so it's still nil when you're trying to add to it.

self.responses = [NSMutableArray array];
//now you can add to it.
like image 45
omz Avatar answered Sep 18 '22 12:09

omz