-1

My goal is to add a string to array, and I do that in a method which I call.

In this method, I get a null value in the array, and don't know why. I have this at the start of my class:

NSMutableArray *listOfEvents;

and a method which I call on each event:

-(void)EventList
{
    [listOfEvents addObject:@"ran"];
    NSLog(@"%@", listOfEvents);     
}

I get (null) in the log.

If I put the array definition NSMutableArray *listOfEvents; in the function body, I get the string value @"ran", each time, so the array always has only one value, instead of having many strings named @"ran".

What's wrong with this? It seems that I can't understand something about arrays, even though I have read the documents a number of times.

jscs
  • 63,694
  • 13
  • 151
  • 195
Curnelious
  • 1
  • 16
  • 76
  • 150
  • possible duplicate of [NSMutableArray addObject not working](http://stackoverflow.com/questions/1827058/) or [Having Problems with Adding Objects to NSMutableArray](http://stackoverflow.com/questions/4716876/) or [NSMutableArray addObject not affecting count](http://stackoverflow.com/questions/3683761/) – jscs Aug 19 '11 at 18:16

4 Answers4

5

I'm assuming you haven't initialized listOfEvents.

Make sure you do listOfEvents = [[NSMutableArray alloc] init]; in your class's init method. Also make sure you release it in your class's dealloc method.

thomashw
  • 956
  • 4
  • 7
  • ok i did listOfEvents=[[NSMutableArray alloc] init]; in the function body so then i only get the value ran, but only one value. it wouldnt add this string every time again and again.. why ? – Curnelious Aug 19 '11 at 17:47
  • 2
    If you're running EventList multiple times per instance of your class then it will keep adding the string to the array. If you're running EventList with a new instance of your class each time, then the array is obviously a new array each time as well. – thomashw Aug 19 '11 at 17:50
  • thanks. i couldnt understand you, each time i press a button(i use cocos2d) i call this function. what do i have to change in order to add new values again and again ? it runs as instance of my class. – Curnelious Aug 19 '11 at 18:03
  • It would be helpful if you made a new question that included code for your entire class, as well as where you're calling it from. – thomashw Aug 19 '11 at 18:06
  • 1
    `listOfEvents=[[NSMutableArray alloc] init];` is right, but **you need to move it to your class's `init` method** so it's only called once. – paulmelnikow Aug 19 '11 at 18:21
  • thanks a lot. it worked but i dont know why. can you direct me to read about it more? there is probably something i dont understand about alloc. thanks. – Curnelious Aug 19 '11 at 18:38
3

If you're getting nil in your log message, you need to make sure listOfEvents is non-nil before adding your object. IE:

-(void)EventList
{
    if (listOfEvents == nil) {
        listOfEvents = [[NSMutableArray alloc] init];
    }
    [listOfEvents addObject:@"ran"];
    NSLog(@"%@",listOfEvents);      
}

In Objective-C, messages with void return types sent to nil go to absolutely-silent nowhere-land.

Also, for the sake of balance, be sure you have a [listOfEvents release] call in your dealloc implementation.

Ben Mosher
  • 13,251
  • 7
  • 69
  • 80
  • thanks a lot. i did that, but then i only see the value ran. even if i call it 10 times,only one string value appear-ran. – Curnelious Aug 19 '11 at 17:53
  • 1
    Two minor points: logging `nil` prints `(null)`, and _any_ message sent to `nil` does nothing, no matter the return type of the method. – jscs Aug 19 '11 at 18:14
1

Apparently you're not initializing your array.

NSMutableArray *listOfEvents = [[NSMutableArray alloc] init];

If that's your problem, I suggest reading the docs again. And not the NSMutableArray docs. Go back to The Objective-C Programming Language and others.

sidyll
  • 57,726
  • 14
  • 108
  • 151
1

You need to alloc the NSMutableArray. Try doing this first -

NSMutableArray *listOfEvents = [[NSMutableArray alloc] init];

After this you could do what you what you planned...

Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264