0


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

7
  • If your arrData is 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) Commented Apr 28, 2011 at 12:57
  • what is showing "null"? , your seems ok (both of the options) Commented Apr 28, 2011 at 12:57
  • Are you check what value come on strlast? Commented Apr 28, 2011 at 12:57
  • @Guy it would have to be arrData because adding a nil object to an array results in an exception. Commented Apr 28, 2011 at 12:59
  • arrData is a NSmutable array variable which i have declared as extern const and strlast is NSMutableString Commented Apr 28, 2011 at 13:13

5 Answers 5

1

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.

  1. Using an NSArray instead of an NSMutableArray when mutability is needed.

  2. 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.

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

1 Comment

As he's getting nil (and no exception) it is more likely that his entire array is nil (see bbum/Joe's answers).
1

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];

5 Comments

Why [[NSMutableArray array] retain];? Why not go straight with [[NSMutableArray alloc] init];?
Just the first thing I typed then added a retain, I can change to prevent a needless autoreleased object.
Yeah, technically it doesn't matter. But wouldn't matter for this one either: 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.
Ya I agree I cheated and edited my comment and answer while you were coming up with that great example of how stupidly I was retaining an array :), and the impact is greater than int i = 1 - 1 + 1; The array will have to be added to the autoreleased pool, tracked, and then released for no reason, a lot more cpu cycles than a simple sub call.
Well, autoreleasing is rather cheap, but it adds unnecessary complexity in case of 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. :)
0

Did you allocate an array and assign it to arrData?

Comments

0

Try:

NSMutableArray *arrData = [NSMutableArray array];
NSString *string = @"My string";
[arrData addObject:string];
NSLog(@"%@", [arrData objectAtIndex:0]); //logs "My string"

1 Comment

NSString *string = [[NSString alloc]initWithString:@"My string"]; is pointless. Just do NSString *string = @"My string";
0

If you're using a non-mutable array, you can also use arrayByAddingObject:

arrData = [arrData arrayByAddingObject: strlast];

but a mutable array is probably a better idea.

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.