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.
NSMutableArray *listData = [[NSMutableArray alloc] initWithContentsOfFile:*filepath*]; // listData = null if the input file does not exist. NSString *jobName = [NSString stringWithFormat:@"My New Job"]; [listData addObject:jobName];
The primary difference between NSArray and NSMutableArray is that a mutable array can be changed/modified after it has been allocated and initialized, whereas an immutable array, NSArray , cannot.
The NSMutableArray class declares the programmatic interface to objects that manage a modifiable array of objects. This class adds insertion and deletion operations to the basic array-handling behavior inherited from NSArray . NSMutableArray is “toll-free bridged” with its Core Foundation counterpart, CFMutableArray .
An object representing a static ordered collection, for use instead of an Array constant in cases that require reference semantics.
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);
NSMutableArray *randomSelection = [[NSMutableArray alloc]init]; [randomSelection addObject:@"string1"];
You need to alloc it first.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With