在上述情况下,我建议您创建一个模型类实现来存储所有值。遵循MVC 架构是这里最好的方法。
例如:-
@interface Music : NSObject {}
@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) NSString *artistName;
@property (nonatomic, strong) NSString *albumName;
//etc...
@end
现在,如果您想创建一系列音乐,请执行以下操作:
Music *myMusic1 = [[Music alloc] init];
myMusic1.title = @"name";
myMusic1.artistName = @"artist";//etc.. do this in a loop based on other contents array
NSMutableArray *array = [NSMutableArray arrayWithObjects:myMusic1, myMusic2, ... nil];
现在对这个数组进行排序:
NSArray *sortedArray = [array sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
return [a.title compare:b.title];
}];
如果要使用谓词:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K contains %@", @"title", @"name"];
NSArray *predict = [mainArrayAll filteredArrayUsingPredicate:predicate];
如果要在表格视图中使用这些属性,可以将其用作:
NSString *title = [[sortedArray objectAtIndex:indexPath.row] title];
NSString *artistName = [[sortedArray objectAtIndex:indexPath.row] artistName];