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

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

问题描述

我的目标是在数组中添加一个字符串,我在一个方法中调用。



在这个方法中,数组,不知道为什么。我在类的开始有这个:

  NSMutableArray * listOfEvents; 

以及我针对每个事件调用的方法:

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

我得到(null)如果我把数组定义 NSMutableArray * listOfEvents; 放在函数体中,I每次获取字符串值 @ran,所以数组总是只有一个值,而不是有许多字符串 @ran



这有什么问题?似乎我不能理解数组的一些东西,虽然我已经读了这些文档多次。

解决方案

假设您尚未初始化 listOfEvents



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


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);     
}

I get (null) in the log.

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.

解决方案

I'm assuming you haven't initialized listOfEvents.

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天全站免登陆