本文介绍了NSMutableArrays的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 NSMutableArray 作为一个类的成员变量。

I have an NSMutableArray as a member variable for a class.

在 .h file:

@interface bleh {
NSMutableArray *list;
}

@property (readonly, assign) NSMutableArray *list;

@end

.m file:

@implementation bleh
@synthesize list;
-(void)init;
{
    list = [NSMutableArray arrayWithCapacity:30];
}
@end

现在,我不是一个客观的 - C程序员,所以也许我错过了一些细微差别,但是当我执行以下操作时:

Now, I'm not really an objective-C programmer, so maybe I'm missing some of the nuances, but when I do the following:

NSMutableString *listItem = [NSMutableString stringWithString:@"Foobar"];
[list addObject:listItem];

我的行为很奇怪。也就是说,我正在使用它来保存我最终想要附加到电子邮件的文件列表,然后打开选择器。我得到了一个SIGABRT,经过调试,我发现每当我在列表上操作时,我什么都没得到。 addObject消息根本不会增加 NSMutableArray 的大小。

I'm getting strange behavior. Namely, I'm using this to keep a list of files that I eventually want to attach to an email and then open the picker. I'm getting a SIGABRT, and upon debugging, I find out that whenever I operate on list, I'm getting nothing. addObject messages don't increase the size of the NSMutableArray at all.

我错过了什么吗?有人可以向我展示设置 NSMutableArray 的完整实现,以便在Objective C中的类中进行操作吗?

Am I missing something? Can someone show me a full implementation of setting up an NSMutableArray to be manipulated within a class in Objective C?

谢谢。

PS - 假设我足够聪明,可以将 NSMutableArray 的操作放在一个成员中包含成员变量的类的函数。

PS - Assume that I'm smart enough to put the manipulations of the NSMutableArray inside of a member function for the class containing the member variable.

推荐答案

你是如何实际创建数组的?它有可能被自动释放并消失吗?请记住,如果您使用便捷方法(例如 array 或其他东西)创建它,则需要保留它。

How are you actually creating your array? Is it possible that it's being autoreleased and going away? Remember, if you create it with a convenience method (like array or something) you need to retain it.

这篇关于NSMutableArrays的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 21:41