NSMutableArray addObject 不添加对象,并且对象已分配

发布于 2024-11-10 09:37:03 字数 1766 浏览 0 评论 0原文

我正在尝试在 NSMutableArray 上使用 addObject: 以及我创建的名为 Position 的类。根据日志,我添加到数组中的 Position 实例不是 nil ——我可以将其属性输出到日志中。然而,该数组似乎从未真正占据位置

@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 方法

-(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
    }
}

调用loadPositions:data后,_positions仍然是nil。非常简单;我有一种感觉,这与内存管理有关,但此时我只需要第二双眼睛。我缺少什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫卆 2024-11-17 09:37:03

TT_RELEASE_SAFELY 宏在将其赋值为 nil (IIRC) 之前向其参数发送一条释放消息。为什么 _positions 为零有什么奇怪的?

虽然您可能出于某种原因需要释放 _postitions,但您必须分配并初始化一个新对象并将其引用分配给 _positions,然后才能向其添加项目。

编辑:正如卢克在评论中指出的那样,TT_RELEASE_SAFELY 来自 Three20 库。

#define TT_RELEASE_SAFELY(__POINTER) { [__POINTER release]; __POINTER = nil; }

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; }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文