我使用此代码创建了一个对象的 NSMutableArray
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray * ary1 = [NSArray arrayWithObjects:@"01/07",@"02/07",@"03/07",@"04/07",@"05/07",@"06/07",@"07/07", nil];
NSArray * ary2 = [NSArray arrayWithObjects:@"First",@"Second",@"Third",@"Forth",@"Fifth",@"Sixth",@"Seventh", nil];
NSArray * ary3 = [NSArray arrayWithObjects:@"1000",@"2000",@"3000",@"4000",@"5000",@"6000",@"7000", nil];
tableAry = [[NSMutableArray alloc] init];
for (int i=0; i<ary1.count; i++) {
//cardSummry will hold the data and give back the model to store in array and we can find that value using model
DataModel *dataModel = [[DataModel alloc] init];
dataModel.date = [ary1 objectAtIndex:i];
dataModel.name = [ary2 objectAtIndex:i];
dataModel.ammount = [ary3 objectAtIndex:i];
[tableAry addObject:dataModel];
}
}
这是我的 DataModel 类
.H 文件
#import <Foundation/Foundation.h>
@interface DataModel : NSObject
//this variable is used to get the data from array
@property (nonatomic,strong) NSString *date;
@property (nonatomic,strong) NSString *name;
@property (nonatomic,strong) NSString *ammount;
//this method will genarate a data model which will be added to array for future use
+ (id)cardSummary:(NSString*)date name:(NSString*)name ammount:(NSString*)ammount;
@end
.M 文件
#import "DataModel.h"
@implementation DataModel
@synthesize date,name,ammount;
//this method will genarate a data model which will be added to array for future use
+ (id)cardSummary:(NSString*)date name:(NSString*)name ammount:(NSString*)ammount
{
DataModel *dataModel = [[self alloc] init];
[dataModel setDate:date];
[dataModel setAmmount:ammount];
[dataModel setName:name];
return dataModel;
}
@end
现在我想根据该数组中的名称对其进行排序,我在 SO 中看到了这个问题,它看起来像我的,并使用它的答案代码来解决我的问题,但它对我不起作用,就是这样
[tableAry sortUsingDescriptors:
[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES selector:@selector(caseInsensitiveCompare:)]]];
NSLog(@"tableAry : %@",tableAry);
那么我怎样才能对我的数组进行排序
更新
正如@Martin R 和@Rick 所说,我已经分配了我的数组,但现在我得到了这个错误。
*** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[DataModel caseInsensitiveCompare:]:无法识别的选择器发送到实例 0x7550850”