无法将项目添加到 NSMutableArray ivar [英] Cannot add items to an NSMutableArray ivar

查看:28
本文介绍了无法将项目添加到 NSMutableArray ivar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是将一个字符串添加到数组中,并在我调用的方法中执行此操作.

My goal is to add a string to array, and I do that in a method which I call.

在这个方法中,我在数组中得到一个空值,不知道为什么.我在上课时就有这个:

In this method, I get a null value in the array, and don't know why. I have this at the start of my class:

NSMutableArray *listOfEvents;

以及我在每个事件上调用的方法:

and a method which I call on each event:

-(void)EventList
{
    [listOfEvents addObject:@"ran"];
    NSLog(@"%@", listOfEvents);     
}

我在日志中得到 (null).

如果我把数组定义NSMutableArray *listOfEvents;放在函数体中,我每次都会得到字符串值@"ran",所以数组总是有只有一个值,而不是有许多名为 @"ran" 的字符串.

If I put the array definition NSMutableArray *listOfEvents; in the function body, I get the string value @"ran", each time, so the array always has only one value, instead of having many strings named @"ran".

这有什么问题吗?好像看不懂数组的东西,看了好几遍文档.

What's wrong with this? It seems that I can't understand something about arrays, even though I have read the documents a number of times.

推荐答案

我假设你还没有初始化 listOfEvents.

I'm assuming you haven't initialized listOfEvents.

确保在类的 init 方法中执行 listOfEvents = [[NSMutableArray alloc] init];.还要确保在类的 dealloc 方法中释放它.

Make sure you do listOfEvents = [[NSMutableArray alloc] init]; in your class's init method. Also make sure you release it in your class's dealloc method.

这篇关于无法将项目添加到 NSMutableArray ivar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆