NSMutableArray addObject不影响计数?

时间:2010-09-10 10:09:23

标签: objective-c nsmutablearray

有人能告诉我为什么即使我正在添加对象,log [self.giftees count]仍会返回0吗?

头:

#import <UIKit/UIKit.h>

@interface Test2AppDelegate : NSObject <UIApplicationDelegate>  
{
    UIWindow *window;
    NSMutableArray *giftees;
}

@property (nonatomic, retain) UIWindow *window;
@property (nonatomic, retain) NSMutableArray *giftees;

@end

从didFinishLaunchingWithOptions调用:

- (void)bootstrapGiftees
{
    NSArray *gifteeNames = [NSArray arrayWithObjects:@"Jesse",,nil];

    for (NSString *gifteeName in gifteeNames)
    {
        GifteeModel *g = [[GifteeModel alloc] init];
        g.name = gifteeName;

        [self.giftees addObject:g];
        NSLog(@"giftees count = %d", [self.giftees count]);
        [g release];
}
}

2 个答案:

答案 0 :(得分:6)

“礼品”是否已初始化?如果它是零,[giftees count]也将返回0

答案 1 :(得分:2)

因为你很可能根本没有初始化giftees数组,所以当代码运行时它仍然是零。