热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

NSMutableArrayaddObject似乎不起作用-NSMutableArrayaddObjectdoesn'tseemtowork

ImreallynewtoiOSdevelopmentandImtryingtocreateasimpletableview,soIaddedaTableV

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?

我在这里错过了什么吗?

2 个解决方案

#1


2  

Have you initialize your activity array before adding object to it??
In viewDidLoad initialize your activity array as follows :-

在向对象添加对象之前,是否已初始化活动数组?在viewDidLoad中,按如下方式初始化您的活动数组: -

self.activity = [[[NSMutableArray alloc]init]autorelease];

#2


2  

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,则无需自动释放。


推荐阅读
author-avatar
男孩艹艹
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有