-1

我正在尝试NSMutableArray使用以下结构对 a 进行排序:

(
{
    Code = "390954-150";
    Size = "35";
},
{
    Code = 790540MSO;
    Size = "30";
}
)

我正在尝试根据 Code 值进行排序,NSMutableArray称为arrayProduct

//Sort array by Code
NSSortDescriptor *aSortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"Code" ascending:YES];
[arrayProduct sortUsingDescriptors:[NSArray arrayWithObject:aSortDescriptor]];
//

但是上面的代码抛出了以下异常:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFArray replaceObjectAtIndex:withObject:]: mutating method sent to immutable object'
4

1 回答 1

2

你的arrayProduct样子NSArray

尝试将其转换为NSMutableArrayand :

NSMutableArray *mutableProducts = [NSMutableArray arrayWithArray:arrayProduct];
[mutableProducts sortUsingDescriptors:[NSArray arrayWithObject:aSortDescriptor]];
于 2013-06-26T17:23:21.680 回答