iPad large NSArray - initWithObjects vs. ArrayWithObjects
can anyone clear this up for开发者_StackOverflow中文版 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 anself.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.
EDIT:
#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
}
as I said: it crashes on the device with self.customerRatingKeys = [[NSArray alloc]initWithObjects:
but works with *self.customerRatingKeys = [[NSArray arrayWithObjects...`
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.
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.
精彩评论