为什么当数据应该为3时,我的数组中的对象数返回0?

时间:2017-08-23 22:42:02

标签: objective-c arrays count

int main(int argc,const char * argv []){     @autoreleasepool {

    NSMutableArray *myMute;
    NSString *stringOne = @"This is string 1";
    NSString *stringTwo = @"This is string 2";
    NSString *stringThree = @"This is string 3";

    [myMute addObject: stringOne];
    [myMute addObject: stringTwo];
    [myMute addObject: stringThree];

    NSLog(@"There are %li objects in the myMute array", [myMute count]);


    return 0;
}

}

1 个答案:

答案 0 :(得分:-1)

只需分配你创建nil数组的数组,你必须用new或alloc

分配内存
NSMutableArray *myMute = [NSMutableArray new];

    NSString *stringOne = @"This is string 1";
    NSString *stringTwo = @"This is string 2";
    NSString *stringThree = @"This is string 3";

    [myMute addObject: stringOne];
    [myMute addObject: stringTwo];
    [myMute addObject: stringThree];

    NSLog(@"There are %li objects in the myMute array", [myMute count]);


    return 0;