31

I have an NSMutableArray named randomSelection:

NSMutableArray *randomSelection;

I am then trying to add strings to this array if certain criteria are met:

[randomSelection addObject:@"string1"];

I am then trying to output the string, to determine if it has added it:

NSString *test = [randomSelection objectAtIndex:0];
NSLog(test);

However nothing is being output to the error log and I can't figure out why.

Any help/hints appreciated.

4
  • Are you sure that the certain critera to add the string are met? Otherwise, maybe you should try NSLog(@"%@", test); Commented Dec 12, 2012 at 6:56
  • Yes the criteria is met. Just below NSLog(test) i have placed: NSLog("Made It!"); and that is being displayed. Commented Dec 12, 2012 at 7:00
  • do i need to initialise the NSMutable array and alocate with ample space or something? I thought that was the point of this addObject function so that i dont have to do that. Commented Dec 12, 2012 at 7:01
  • @David Brunow Please read question before putting any comments or ans.... this guy had not allocate "NSMutableArray *randomSelection;", so how he expect to get array object.... Commented Dec 12, 2012 at 7:24

6 Answers 6

68

I think you are missing to allocate the memory for array. So try this

NSMutableArray *randomSelection = [[NSMutableArray alloc] init];
[randomSelection addObject:@"string1"];
NSString *test = [randomSelection objectAtIndex:0];
NSLog(test);
Sign up to request clarification or add additional context in comments.

2 Comments

@SamBo did indeed forget to allocate memory and initialize it. Right now the Array gets declared, but not defined.
@ophychius Also had the same problem. At least now I know that I need to allocate memory for the array first. But isn't there a more generic way to specify that you only want to add strings in the array?
5
NSMutableArray *randomSelection =  [[NSMutableArray alloc]init];
[randomSelection addObject:@"string1"];

You need to alloc it first.

Comments

2

First allocate the array using following statement & then objects in it.

NSMutableArray *randomSelection =  [[NSMutableArray alloc] init];
[randomSelection addObject:[NSString stringWithFormat:@"String1"]];
[randomSelection addObject:[NSString stringWithFormat:@"String2"]];
NSLog(@"Array - %@", randomSelection);

This will definitely solves your problem.

Comments

1

Just allocate your NSMutableArray. You'll get solved your problem.

Comments

1

Try this:

NSMutableArray *randomSelection = [[NSMutableArray alloc]init]; 
[randomSelection addObject:@"string1"];

Comments

0

Swift :

var randomSelection: [AnyObject] = [AnyObject]()
randomSelection.append("string1")
let test: String = randomSelection[0] as! String
print(test)

OR

let array : NSMutableArray = []
array.addObject("test String")
print(array)

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.