我新的代碼,我不明白爲什麼這工作得很好,當我鍵入它放在一個主文件和啓動命令行的項目:如預期填入數字數組addOBject返回NULL?
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSMutableArray *myArray= [[NSMutableArray alloc] init];
for (int anID = 0; anID < (20); anID++)
{
[myArray addObject:[NSNumber numberWithInt:anID]];
}
NSLog(@"%@", myArray);
return 0;
}
}
和回報:
2015-10-21 21:10:01.783 quicktry[3268:727372] (
0,
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19
)
Program ended with exit code: 0
但如果我這樣做,作爲一類完全一樣的東西不起作用:
@interface ViewController()
@property NSMutableArray *myArray;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
for (int anID = 0; anID < (20); anID++)
{
[self.myArray addObject:[NSNumber numberWithInt:anID]];
}
NSLog(@"%@", self.myArray);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
這將返回NULL。據我所知,這是完全相同的代碼。爲什麼不在這裏工作?
當我說退貨我的意思是註銷到控制檯。 – Dave
您沒有初始化'self.myArray'。在nil上調用任何方法是沒有用的。 – Andy