0

I'm trying to use addObject: on an NSMutableArray with a class I've created, called Position. According to the logs, the Position instance that I am adding to the array is not nil -- I can output its properties to the log. However, the array never seems to actually take the Position.

@implementation Position

@synthesize tradeDirection, instrument, quantity, fill, profit, positive, index;

-(id)initWithArray:(NSArray*)data
{
    self = [super init];
    if (self)
    {
        self.tradeDirection = [data objectAtIndex:0];
        self.instrument     = [data objectAtIndex:1];
        self.quantity       = [data objectAtIndex:2];
        self.fill           = (NSNumber*)[data objectAtIndex:3];
        self.profit         = [data objectAtIndex:4];
        self.positive       = [@"True" isEqualToString:[data objectAtIndex:5]];
        self.index          = (NSNumber*)[data objectAtIndex:6];
    }
    return self;
}

+(Position*)createPositionWithString:(NSString*)data
{
    return [[[self alloc] initWithArray:[data componentsSeparatedByString:@"#"]] autorelease];
}

@end

loadPositions:data method

-(void)loadPositions:(NSString*)data
{
    TT_RELEASE_SAFELY(_positions);
    NSArray* dataArray = [data componentsSeparatedByString:@";"];
    for (int i = 2; i <= [dataArray count] - 2; i++)
    {
        //TODO: Discover why this does not add a position.
        Position* newPos = [Position createPositionWithString:[dataArray objectAtIndex:i]];
        [_positions addObject:newPos]; //position never actually gets added
        NSLog(@"Position direction: %@", newPos.tradeDirection); //logs what I expect, i.e. newPos is not nil
    }
}

After calling loadPositions:data, _positions is still nil. Pretty straightforward; I have a feeling this has something to do with memory management, but at this point I just need a second set of eyes. What am I missing?

2
  • 6
    Where do you create _positions array? Commented May 30, 2011 at 20:17
  • 1
    Almost certainly you are not instantiating/retaining the _positions array correctly. Need to see more code surrounding this bit as everything else you've posted looks fine. Commented May 30, 2011 at 20:21

1 Answer 1

5

The TT_RELEASE_SAFELY macro sends a release message to its argument before assigning it to nil (IIRC). Why is it any surprise that _positions is nil?

While you may need to release _postitions for whatever reason, you must allocate and initialize a new object and assign its reference to _positions before you can add items to it.

edit: as Luke indicated in his comment, TT_RELEASE_SAFELY is from the three20 library.

#define TT_RELEASE_SAFELY(__POINTER) { [__POINTER release]; __POINTER = nil; }
Sign up to request clarification or add additional context in comments.

3 Comments

This. Might I also add that if using non-standard library code (such as TT_RELEASE_SAFELY, is that from the Three20 library?) it helps to explain what it does. Not everybody uses these libraries.
Ah yes, the timely programming paradigm classic of setting an object to nil resulting in the object being nil.
My comment was aimed at the OP actually but thanks for the clarification.

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.