0

I try to store some Map Informations into a NSMutableArray but it is not working. I think there is only a minor problem that I couldn't find.

At .h File:

@property (retain, nonatomic) NSMutableArray *annotationArray;

at .m File:

    MapPoint *placeObject = [[MapPoint alloc] initWithName:title subtitle:subtitle coordinate:loc];
    [annotationArray addObject:placeObject];
    NSLog(@" count: %lu", (unsigned long)[annotationArray count]); // always "0"

what did I wrong?

4
  • Did you initialize the array ? Commented Apr 15, 2013 at 11:57
  • Where u have allocated memory for the NSMUtable array? Commented Apr 15, 2013 at 11:57
  • Yes I forgot to allocate... Commented Apr 15, 2013 at 12:00
  • ok correct your code... Commented Apr 15, 2013 at 12:01

4 Answers 4

3

It seems that you may have forgotten to initialise the array. Try to add the following line before trying to add an object to it:

[self setAnnotationArray:[[NSMutableArray alloc] init]];
Sign up to request clarification or add additional context in comments.

Comments

1

I would need to see more code, but I think it's most likely your not setting up annotationArray. Are you writing _annotationArray = [[NSMutableArray alloc] init] anywhere?

If not, the place for it is the class init method, or you could write in the function:

if (annotationArray == nil) {
_annotationArray = [[NSMutableArray alloc] init]
}

You could also use [self setAnnotationArray:[[NSMutableArray alloc] init]] instead of _annotationArray, but it depends on your synthesize statement.

I would suggest that you don't just initialize it in your function without checking if it's already been set first, because then you run the risk of overriding something else when your code gets more complex.

Comments

0

I think you missed this:

_annotationArray=[[NSMutableArray alloc]init];

17 Comments

What do you want? I gave an answer with the information that was provided.
There is a reason why SO has comments. although this probably is the answer this encourages many answers which in some cases might not be the right ones until the OP clearly states what he did. They could easily ask it in a comment and reply upon the OP's reply
@giorashc it is a bit mad to downvote all of us. However I've learned the lesson: Ask first before posting a trivial answer. Its not nice what you did in any case...
I'm sorry to say that this code won't work as it is a property and you are not accessing it through the getter nor the instance variable.
@giorashc: from the symptoms it can only be a un-initialized array. so requesting further information is a waste of time.
|
0

at .m File: add after header

@synthesize annotationArray=_annotationArray;

- (void)loadView 
{
self. annotationArray =[NSMutableArray array];
}

1 Comment

explicite synthesis isnt needed anymore.

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.