在循环中将对象添加到 NSMutableArray
我在让 NSMutableArray 工作时遇到问题。我简化了代码来说明我的问题。我有一个类,我将其用作数据容器。
@interface Question : NSObject {
int questionID;
NSString* text;
}
@property int questionID;
@property(nonatomic,retain)NSString* text;
@end
我正在创建此容器的不同实例,如下所示:
.h
#import "Question.h"
@interface testViewController : UIViewController {
NSMutableArray* questions;
}
-(IBAction) start;
.m
....
- (void)viewDidLoad {
[super viewDidLoad];
questions=[[NSMutableArray alloc]init];
for (int i=0; i<5; i++) {
int questionID=i;
Question* question=[[Question alloc]init];
question.questionID=questionID;
question.text=[NSString stringWithFormat:@"text %d",i];
[questions addObject:question];
[question release];
}
}
....
-(IBAction) start{
for (int i=0; i<[questions count]; i++) {
Question *theQuestion;
theQuestion=(Question*)[questions objectAtIndex:i];
NSLog(@"%d",theQuestion.questionID);
NSLog(@"%@",theQuestion.text);
NSLog(@"----------------------");
}
}
在 ViewDidLoad 中,问题的实例在循环中创建,并将它们添加到 NSMutableArray 问题中。添加问题后,将其发布。填充 NSMutableArray 后,通过按 UI 上的按钮来执行函数启动。该函数应该打印出不同问题的内容。
问题是,当执行函数 start 时,NSMutableArray 问题没有之前存储的内容,并且程序崩溃了。
NSLog(@"%d",theQuestion.questionID);
有趣的是,如果我不释放函数 viewDidLoad 上的问题,那么一切都会正常。但是,我会错过释放之前分配的变量,这应该会导致泄漏。
有人知道如何正确执行此操作吗?
I have problems getting NSMutableArray to work. I simplified the code for illustrating my problem. I have a class, which I use as a Data container.
@interface Question : NSObject {
int questionID;
NSString* text;
}
@property int questionID;
@property(nonatomic,retain)NSString* text;
@end
I am creating different instances of this container as follows:
.h
#import "Question.h"
@interface testViewController : UIViewController {
NSMutableArray* questions;
}
-(IBAction) start;
.m
....
- (void)viewDidLoad {
[super viewDidLoad];
questions=[[NSMutableArray alloc]init];
for (int i=0; i<5; i++) {
int questionID=i;
Question* question=[[Question alloc]init];
question.questionID=questionID;
question.text=[NSString stringWithFormat:@"text %d",i];
[questions addObject:question];
[question release];
}
}
..........
-(IBAction) start{
for (int i=0; i<[questions count]; i++) {
Question *theQuestion;
theQuestion=(Question*)[questions objectAtIndex:i];
NSLog(@"%d",theQuestion.questionID);
NSLog(@"%@",theQuestion.text);
NSLog(@"----------------------");
}
}
In ViewDidLoad instances of question are created in a loop and they are added to the NSMutableArray questions. After question has been added it is released. After populating the NSMutableArray a function start is executed by pressing a button on the UI. This function should print out the contents of the different questions.
The problem is when the function start is executed the NSMutableArray questions does not have the contents stored before and the program crashes on
NSLog(@"%d",theQuestion.questionID);
The interesting thing is that if I do not release the question on the function viewDidLoad, then everything works fine. However, I would miss releasing a variable I previously allocated and this should lead to a leak.
Does anybody has an idea how to do this properly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的问题很可能出在代码的其他地方,内存问题以奇怪的方式暴露出来。您确定没有其他内容与
问题
及其内容交互吗?我建议您在代码中添加一个断点,并在崩溃时探索其内容。It's quite possible that your issue lies elsewhere in your code, memory issues expose themselves in strange ways. Are you sure nothing else interacts with
questions
and its contents? I suggest you add a breakpoint to the code and explore its contents at the point of the crash.创建问题时删除释放并使用它:
另外,在这里使用自动释放池可能是一个好主意。
When creating Question delete release and use this instead:
Also it might be a good idea to use autorelease pool here.