Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
295 views
in Technique[技术] by (71.8m points)

objective c - Cannot add items to an 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);     
}

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.

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

Just Browsing Browsing

[3] html - How to create even cell spacing within a

1.4m articles

1.4m replys

5 comments

57.0k users

...