NSMutableArray addObject not affecting count?

天大地大妈咪最大 提交于 2019-12-16 20:07:28

问题


Can anyone tell me why logging [self.giftees count] keeps returning 0 even though I'm adding objects to it?

header:

#import <UIKit/UIKit.h>

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

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

@end

called from 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];
}
}

回答1:


Is "giftees" initialized? If it is nil, [giftees count] will return 0 as well




回答2:


Because you most probably never initialized the giftees array at all, so it is still nil when that code runs.



来源:https://stackoverflow.com/questions/3683761/nsmutablearray-addobject-not-affecting-count

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!