English 中文(简体)
crashed on iphone but not on simulator
原标题:

It is really mind boggling to find out many differences between the iphone and the simulators. I spent several hours trying to figure out why my application ran on the simulator but crashed on my iphone device. It turns out the culprit is sortedArrayUsingDescriptors. Are there more get-you like this? Please share with me.

To share with you on the issue and the fixes:


Code crashed on iphone but not simulator

NSSortDescriptor* aDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"count" ascending:NO] autorelease];
NSArray* anNsArray = [[NSArray alloc] init];
NSArray* aSortedNsArray = [[NSArray alloc] init];

aSortedNsArray = [anNsArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:aDescriptor]];

The issue is on [NSArray arrayWithObject:aDescriptor];


The fixes are to create an Array to store it:

NSArray* descriptorArray = [[NSArray alloc] initWithObjects:countDescrp, nil];
aSortedNsArray = [anNsArray sortedArrayUsingDescriptors:descriptorArray];
最佳回答
NSSortDescriptor* aDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"count" ascending:NO] autorelease]; 
NSArray* anNsArray = [[NSArray alloc] init];
NSArray* aSortedNsArray = [[NSArray alloc] init];

aSortedNsArray = [anNsArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:aDescriptor]];

This is a wrong initialization mechanism, and if the code snippet is complete, your problem lies on the anNsArray object that s empty.

You also do not need to initialize the aSortedNsArray.

So it should be:

NSSortDescriptor* sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"count" ascending:NO] autorelease]; 

// Assume you return it as array of objects from a property or method
NSArray *array = [self objects]; 
NSArray *sortedArray = nil;
if ([array count] > 0) {
     sortedArray = [array sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
}

// Then you check the sortedArray
if (sortedArray == nil || [sortedArray count] == 0)
   [self somethingIsWrong];     

arrayWithObject: (autoreleased) or initWithObject: (manual) is just a different way to allocate NSArray object. It won t cause crashes normally. Because what you care about is the sortedArray not retaining the descriptors array object.

问题回答

暂无回答




相关问题
Ruby Interpreter crashes with a certain word

Ok, this one s a little ridiculous, and I m almost afraid no one will believe me. But here it goes: I have written a Ruby Rails application that handles content for tons of domains. Now I know this ...

Java HotSpot error

Curious if anyone could help out in regards to a Java HotSpot dump...saw some reference to head over to the Sun Forums, figured I would try here first...below is the dump... # # An unexpected error ...

NSOperation performSelectorOnMainThread crashes

I m calling a NSOperation from a Subview of a NavigationController. MyOperation *op = [[MyOperation alloc] target:self action:@selector(didFinishOperation)]; The Operation loads some data from a ...

Is this kind of crash report useless?

I tried use "PLCrashReport" to collect the crash info then make the app more stable, but turns out that the report is like this(dont even has a call stack,how am I suppose to use it?): The "Exception:...

Xcode crashes with divide by zero

I downloaded urlcache.zip from http://developer.apple.com/iphone/library/samplecode/URLCache/index.html#//apple_ref/doc/uid/DTS40008061 I opened the project in xcode and clicked on urlcacheconection....

热门标签