2

Hey guys, I have this code within a function inside a class that is a subclass of NSOperation:

//...
@implementation DataLoader

@synthesize addedAnnotations;
@synthesize addedOverlays;
@synthesize loaderFunc;
@synthesize DLDelegate;    
//...
-(id)initWithFunction:(LoaderFunc)func withDelegate:(id)delegate {
    if (self = [super init]) {
        self.addedOverlays = nil;
        self.addedAnnotations = nil;
        self.loaderFunc = func;
        self.DLDelegate = delegate;
        return self;
    }
    return nil;
}
//...
//inside a function
    for (ParkingAnnotations *annotation in fetchedObjects) {
        ParkingAnnotation *parkingAnnot = [[ParkingAnnotation alloc] init];
        workingCoordinate.latitude = [[annotation latitude] doubleValue];
        workingCoordinate.longitude = [[annotation longitude] doubleValue];
        [parkingAnnot setCoordinate:workingCoordinate];
        [parkingAnnot setTitle:[annotation valueForKey:@"lotName"]];
        [parkingAnnot setAnnotationType:[annotation iconTypeRaw]];

        [self.addedAnnotations addObject:parkingAnnot];//parkingAnnot not added to array here
        [parkingAnnot release];
    }
//...

Added annotations is an NSMutable array, I have been walking through this code with the debugger and for some reason the parkingAnnot object is not getting added to the array. Here is the relevant header code for the class:

//...    
@interface DataLoader : NSOperation {
        NSMutableArray *addedAnnotations;
    NSMutableArray *addedOverlays;
    LoaderFunc loaderfunc;
    id <DataLoaderProtocol> DLDelegate;
}

@property (nonatomic, retain) NSMutableArray* addedAnnotations;
@property (nonatomic, retain) NSMutableArray* addedOverlays;
@property (nonatomic) LoaderFunc loaderFunc;
@property (assign) id DLDelegate;
//...

It is an astonishing problem because the function in which I am experiencing the problem was copied from my MapViewController and is essentially the same, but instead of mapView addAnnotation: I am adding to an NSMutable array instead. Any idea of what's up? Thanks in advance!

4
  • can you post your code for ParkingAnnotation initializer? Anything going on there which would cause it to be nil? Commented May 10, 2011 at 13:21
  • 1
    You have allocated self.addedAnnotations. please alloc it before For loop. like self.addedAnnotations =[[nsmutablearray alloc] init]; Commented May 10, 2011 at 13:21
  • At the start in your init function you have the line: self.addedAnnotations = nil; Are you actually setting this variable to be a Mutable Array at all anywhere else? Or is it nil when you're trying to add to it? Commented May 10, 2011 at 13:23
  • Yeah you guys had the right idea, I didn't initialize it before the for loop, foolish mistake. Thanks for the responses. Commented May 10, 2011 at 13:40

1 Answer 1

4

Where are you actually instantiating the addedAnnotations array? I only see it being assigned nil in your initialize function, maybe it should change to something like:

self.addedAnnotations = [[[NSMutableArray alloc] init] autorelease];
Sign up to request clarification or add additional context in comments.

5 Comments

Damn, I knew it was something simple like that, thanks for helping me see it. That did the trick!
@PeyloW Why 32? For memory-alignment purposes?
No particular reason this time. Some number is required, and 32 felt good. In real world you want a number that is large enough to not need the mutable array capacity to be increased in a normal use case. Since increasing the size of the array is expensive.
Increasing the size of the array is not necessarily expensive, nor does every addition of an object incur any expense. Unless you absolutely positively know how many objects you need, don't bother. Such optimizations based on assumptions about implementation details of the frameworks most often leads to code that is slower than code that leaves it up to the framework to choose the optimal implementation.
@PeyloW: I'm not an authority here, but in my opinion, editing someone's answer to change the meaning so drastically is, frankly, rude. From the "How to Edit" guidelines: "*clarify meaning without changing it *always respect the original author" I've rolled back the change you made to iceydee's original post.

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.