[英]NSMutableArray addObject doesn't seem to work
I'm really new to iOS development and I'm trying to create a simple table view, so I added a Table View Controller, added my custom class to it and now I was trying to add items. 我真的很喜欢iOS开发,我正在尝试创建一个简单的表视图,所以我添加了一个表视图控制器,添加了我的自定义类,现在我尝试添加项目。
So I created a new array: 所以我创建了一个新数组:
@property (nonatomic, strong) NSMutableArray *activity;
And I the viewDidLoad, I tried adding some items to the array: 我是viewDidLoad,我尝试在数组中添加一些项目:
for(int i = 0; i < 20; i++)
{
NSLog(@"Current item: %u", i);
Item *item = [[Item alloc] init];
item.time = @"14:30";
item.text = @"A simple test";
item.startedAgo = @"yesterday";
item.project = @"testProject";
[self.activity addObject:item];
}
NSLog(@"Activity count: %u", [self.activity count]);
The log seems to be fine, excpet when I try to output the lenght of the array, it stays 0 日志似乎很好,当我尝试输出数组的长度时,它会保持为0
2012-05-02 14:12:50.772 coop_dev[15352:f803] Current item: 0 2012-05-02 14:12:50.772 coop_dev [15352:f803]当前项目:0
... ...
2012-05-02 14:12:50.783 coop_dev[15352:f803] Current item: 19 2012-05-02 14:12:50.783 coop_dev [15352:f803]当前项目:19
2012-05-02 14:12:50.784 coop_dev[15352:f803] Activity count: 0 2012-05-02 14:12:50.784 coop_dev [15352:f803]活动计数:0
Am I missing something here? 我在这里错过了什么吗?
Have you initialize your activity array before adding object to it?? 在向对象添加对象之前,是否已初始化活动数组?
In viewDidLoad initialize your activity array as follows :- 在viewDidLoad中,按如下方式初始化您的activity数组: -
self.activity = [[[NSMutableArray alloc]init]autorelease];
Make sure you initialise your array before using it 确保在使用之前初始化阵列
self.activity = [[NSMutableArray alloc] init];
If your using ARC there is no need for autorelease. 如果您使用ARC,则无需自动释放。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.