iPad large NSArray - initWithObjects vs. ArrayWithObjects [英] iPad large NSArray - initWithObjects vs. ArrayWithObjects
问题描述
任何人都可以为我清除这个吗?
我正在构建一个iPad应用程序,它有一个TableViewController,可以显示1000到2000个字符串之间的内容。
我在Singleton中有那些NSStrings。
在Singleton的init方法中我初始化一个包含所有数据的数组(不一定是最后的方法) - 只是快速复制和粘贴测试)
我做了一个 self.someArray = [[NSArray alloc] initWithObjects:
后面跟着大量的字符串,接下来是nil。
在模拟器中运行良好 - 但是在应用程序启动时在iPad上遇到了糟糕的访问崩溃
如果我使用便捷方法 [ NSArray arrayWithObjects:
相反 - 它工作正常。
我调查了仪器,应用程序的整体内存占用量大约为2.5 MB。
现在我不知道它为什么单向而不是另一种方式。
编辑:
can anyone clear this up for me ?
I am building an iPad App that has a TableViewController that is supposed to show something between 1000 and 2000 strings.
I have those NSStrings in a Singleton.
In the init Method of the Singleton I initialize an Array that holds all the data ( does not have to be the final way to do it - was just a quick copy and paste for testing )
I did an self.someArray = [[NSArray alloc]initWithObjects:
followed by the large number of strings, followed by nil.
that worked fine in the simulator - but crashed with bad access on the iPad right on Application startup
If I use the convenience method [NSArray arrayWithObjects:
instead - it works fine.
I looked into Instruments and the overall memory footprint of the App is just about 2,5 MB.
Now I don't know why it works the one way but not the other.
#import "StaticValueContainer.h"`
static StaticValueContainer* instance = nil;
@implementation StaticValueContainer
@synthesize customerRatingKeys;
+(StaticValueContainer*)sharedInstance
{
if (instance == nil){
instance = [[StaticValueContainer alloc]init];
}
return instance;
}
-(id)init
{
if ( ( self = [super init] ))
{
[self initCustomerRatingKeys];
}
return self;
}
-(void)init customerRatingKeys
{
self.customerRatingKeys = [[NSArray alloc]initWithObjects:
@"string1",
....
@"string1245"
,nil
}
正如我所说:它在设备上崩溃 self.customerRatingKeys = [[NSArray alloc] initWithObjects:
但是可以使用* self.customerRatingKeys = [[NSArray arrayWithObjects ...`
as I said: it crashes on the device with self.customerRatingKeys = [[NSArray alloc]initWithObjects:
but works with *self.customerRatingKeys = [[NSArray arrayWithObjects...`
推荐答案
嗯,它们之间没有太大区别: arrayWithObjects
返回一个你不需要自己发布的自动发布的数组(除非你随后保留它),并且 initWithObjects
返回一个数组,然后必须释放以避免内存泄漏。性能方面,它们之间没有区别。
Well, there isn't much difference between them: arrayWithObjects
returns an auto-released array that you don't need to release yourself (unless you subsequently retain it), and initWithObjects
returns an array you must then release to avoid a memory leak. Performance wise there is no difference between them.
如果您使用 initWithObjects
但不是 arrayWithObjects
您的代码中可能存在某种内存管理错误。如果您发布代码本身,您可能会得到更准确的响应。
I would suggest if you're getting a bad access error using initWithObjects
but not with arrayWithObjects
there might be some sort of memory management error in your code. If you post the code itself you'll probably get a more exact response.
这篇关于iPad large NSArray - initWithObjects vs. ArrayWithObjects的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!