Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/99.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用NSSortDescriptor对iOS中的包装器对象数组进行排序_Ios_Objective C_Arrays_Sorting_Nssortdescriptor - Fatal编程技术网

使用NSSortDescriptor对iOS中的包装器对象数组进行排序

使用NSSortDescriptor对iOS中的包装器对象数组进行排序,ios,objective-c,arrays,sorting,nssortdescriptor,Ios,Objective C,Arrays,Sorting,Nssortdescriptor,我正在尝试对UserWrapper对象数组进行排序。包装器对象包含对象User,用户对象包含属性UserName(我想按其排序) 对一组用户()进行排序非常简单,但是添加的UserWrapper层使事情变得复杂。救命啊 以下是我的代码,它适用于一个简单的用户数组: NSSortDescriptor *nameDescriptor = [[NSSortDescriptor alloc] initWithKey:@"UserName"

我正在尝试对UserWrapper对象数组进行排序。包装器对象包含对象User,用户对象包含属性UserName(我想按其排序)

对一组用户()进行排序非常简单,但是添加的UserWrapper层使事情变得复杂。救命啊

以下是我的代码,它适用于一个简单的用户数组:

NSSortDescriptor *nameDescriptor = [[NSSortDescriptor alloc] initWithKey:@"UserName"
                                                               ascending:YES
                                                                selector:@selector(localizedCaseInsensitiveCompare:)] ; 

NSArray *descriptors = [NSArray arrayWithObject:nameDescriptor];
NSMutableArray *contactsStartingWithKey = [nameIndexesDictionary objectForKey:aKey];
[contactsStartingWithKey sortUsingDescriptors:descriptors]; // Exception thrown here because UserName is not a property of UserWrapper, but of UserWrapper.User
在您的情况下,排序描述符的键参数也可以通过键路径进行排序:

[[NSSortDescriptor alloc] initWithKey:@"User.UserName"
                            ascending:YES
                             selector:@selector(localizedCaseInsensitiveCompare:)] ; 

尽管
nssortddescriptor
的API和文档有点不一致,但“key”参数实际上是一个密钥路径。因此,您只需指定
@“User.UserName”
作为键,您的代码就可以工作了