i wanted to know how to add strings into an array.
I used the following methods but it is showing null.
1) [arrData addObject:[NSString stringWithString:strlast]];
2) [arrData addObject:strlast];
Thanks in advance
You can't add anything to an NSArray once it's created. You need to use an NSMutableArray if you want to make changes to it.
Update: You may actually have two problems.
Using an NSArray instead of an NSMutableArray when mutability is needed.
Not initializing the array object (either kind). If arrData is nil, you can happily send as many messages as you want to nil. Nothing will happen.
nil (and no exception) it is more likely that his entire array is nil (see bbum/Joe's answers).If it is showing null (nil) you need to make sure you set arrData somewhere in your code before trying to addObject:.
arrData = [[NSMutableArray alloc] init];
Also strlast is a string so use your second example, the first example is pointless.
[arrData addObject:strlast];
[[NSMutableArray array] retain];? Why not go straight with [[NSMutableArray alloc] init];?int i = 1 - 1 + 1;. Yet kinda pointless, isn't it? (and internally basically the equivalent of [[NSMutableArray array] retain]). For sake of style I'd always go the less bloated way. In particular as OP seems to be kinda new to ObjC. For non-novices it really doesn't matter enough to really care about.EXC_BAD_ACCESS debugging (code failure being postponed by autorelease pool). And int i = 1 - 1 + 1; has basically no overhead, as the compiler would optimize it anyway. ;) Whatever. (Oh and I didn't mean to call you names and/or stupid in any way. Was merely trying to reduce confusion for OP.) Have a nice day, Joe. :)Try:
NSMutableArray *arrData = [NSMutableArray array];
NSString *string = @"My string";
[arrData addObject:string];
NSLog(@"%@", [arrData objectAtIndex:0]); //logs "My string"
NSString *string = [[NSString alloc]initWithString:@"My string"]; is pointless. Just do NSString *string = @"My string";
arrDatais a valid NSMutableArray, then your code is absolutely right. You'll need to give a bit more context. (array creation, object adding, object retrieval, etc)