Note that there are some explanatory texts on larger screens.

plurals
  1. POStoring Sorted Arrays Causing EXC_BAD_ACCESS Error
    text
    copied!<p>Given a basic key/value array, I'm wanting to store two sorted arrays based on the original array: one array will be sorted by name, and the other by age.</p> <p>The arrays seem to be sorting correctly when I output them to the log; however, when I try to access them elsewhere in the code, I'm receiving a EXC_BAD_ACCESS error.</p> <p>Here's what I have so far:</p> <pre><code>// MyController.h @interface MyController : UIViewController { NSMutableArray *originalArray; NSMutableArray *nameArray; NSMutableArray *ageArray; } @property (nonatomic, retain) NSMutableArray *originalArray; @property (nonatomic, retain) NSMutableArray *nameArray; @property (nonatomic, retain) NSMutableArray *ageArray; -(void)someRandomMethod; @end // MyController.m #import "MyController.h" @implementation MyController @synthesize originalArray; @synthesize nameArray; @synthesize ageArray; -(void)viewDidLoad { // originalArray = ( // { // "name" = "Sally"; // "age" = 18; // }, // { // "name" = "Chad"; // "age" = 26; // }, // { // "name" = "Carla"; // "age" = 24; // }, // ) // sort by name NSSortDescriptor *sortByNameDescriptor; sortByNameDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"name" ascending:NO] autorelease]; NSArray *sortByNameDescriptors = [NSArray arrayWithObject:sortByNameDescriptor]; nameArray = [originalArray sortedArrayUsingDescriptors:sortByNameDescriptors]; // sort by age NSSortDescriptor *sortByAgeDescriptor; sortByAgeDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"age" ascending:NO] autorelease]; NSArray *sortAgeDescriptors = [NSArray arrayWithObject:sortByAgeDescriptor]; ageArray = [originalArray sortedArrayUsingDescriptors:sortByAgeDescriptors]; [super viewDidLoad]; } -(void)someRandomMethod { // whenever I try to access the sorted arrays, I receive the EXC_BAD_ACCESS error [[nameArray objectAtIndex:0] valueForKey:@"name"]; [[ageArray objectAtIndex:0] valueForKey:@"age"]; } -(void)viewDidUnload { self.originalArray = nil; self.nameArray = nil; self.ageArray = nil; [super viewDidUnload]; } - (void)dealloc { [originalArray release]; [nameArray release]; [ageArray release]; [super dealloc]; } @end </code></pre> <p>Any ideas?</p> <p><strong>UPDATE:</strong> Thanks to @robin, by changing the code above to the code below, everything works great:</p> <pre><code>// sort by name NSSortDescriptor *sortByNameDescriptor; sortByNameDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"name" ascending:NO] autorelease]; NSArray *sortByNameDescriptors = [NSArray arrayWithObject:sortByNameDescriptor]; nameArray = [[NSMutableArray alloc] initWithArray:[originalArray sortedArrayUsingDescriptors:sortByNameDescriptors]]; // sort by age NSSortDescriptor *sortByAgeDescriptor; sortByAgeDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"age" ascending:NO] autorelease]; NSArray *sortAgeDescriptors = [NSArray arrayWithObject:sortByAgeDescriptor]; ageArray = [[NSMutableArray alloc] initWithArray:[originalArray sortedArrayUsingDescriptors:sortByAgeDescriptors]]; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload