NSMutableArray addObject 不添加对象,并且对象已分配
我正在尝试在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
TT_RELEASE_SAFELY 宏在将其赋值为 nil (IIRC) 之前向其参数发送一条释放消息。为什么 _positions 为零有什么奇怪的?
虽然您可能出于某种原因需要释放 _postitions,但您必须分配并初始化一个新对象并将其引用分配给 _positions,然后才能向其添加项目。
编辑:正如卢克在评论中指出的那样,TT_RELEASE_SAFELY 来自 Three20 库。
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.