Specifying Sorts Using NSSortDescriptor
Let’s assume, as an example, that we have an array (an instance of NSArray) containing instances of a custom class, Employee (that meets the requirements set out in “Requirements of Collection Objects”). The Employee class has attributes for an employee’s first and last name (instances of NSString), date of hire (an instance of NSDate), and age (an instance of NSNumber).
1:Sorting the array by the age key
ageDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"age"
ascending:YES] autorelease];
sortDescriptors = [NSArray arrayWithObject:ageDescriptor];
sortedArray = [employeesArray sortedArrayUsingDescriptors:sortDescriptors];
2:Sorting the array by the age and date of hire key
ageDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"age"
ascending:YES] autorelease];
hireDateDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"hireDate"
ascending:YES] autorelease];
sortDescriptors = [NSArray arrayWithObjects:ageDescriptor, hireDateDescriptor, nil];
sortedArray = [employeesArray sortedArrayUsingDescriptors:sortDescriptors];
Specifying Custom Comparisons
Sorting the array using a localized case insensitive comparison
lastNameDescriptor = [[[NSSortDescriptor alloc]
initWithKey:@"lastName"
ascending:YES
selector:@selector(localizedCaseInsensitiveCompare:)] autorelease];
firstNameDescriptor = [[[NSSortDescriptor alloc]
initWithKey:@"firstName"
ascending:YES
selector:@selector(localizedCaseInsensitiveCompare:)] autorelease];
sortDescriptors = [NSArray arrayWithObjects:lastNameDescriptor,
firstNameDescriptor, nil];
sortedArray = [peopleArray sortedArrayUsingDescriptors:sortDescriptors];
The Foundation classes that have methods that can be used with sort descriptors are listed in Table 1.
Table 1 Common Foundation classes and comparison methods
写道
Comparison Method Supporting Classes
compare: NSString, NSMutableString, NSDate, NSCalendarDate, NSValue (scalar types and unsigned char only), NSNumber
caseInsensitiveCompare: NSString, NSMutableString
localizedCompare: NSString, NSMutableString
localizedCaseInsensitiveCompare: NSString, NSMutableString
分享到:
相关推荐
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"someProperty" ascending:YES]; NSArray *sortedArray = [objects sortedArrayUsingDescriptors:@[sortDescriptor]]; ``` 在...
当需要根据多个属性进行排序时,可以使用`sortUsingDescriptors:`方法,它接受一个NSArray对象,其中包含NSSortDescriptor对象,每个描述符代表一种排序方式。例如: ```objc NSMutableArray *descriptors = @[ ...
fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]]; PHFetchResult*> *assets = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:...
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "localizedTitle", ascending: true)] let smartAlbums = PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .any, options: ...
let sortDescriptor = NSSortDescriptor(key: "publishDate", ascending: false) fetchRequest.sortDescriptors = [sortDescriptor] ``` 10. **使用 NSFetchedResultsController** 对于复杂的表格视图,通常...
例如,创建一个`NSSortDescriptor`对象,其key为`creationDate`,表示图片的创建日期,选择降序排列(最新的在前)。 ```swift let sortDescriptor = NSSortDescriptor(key: "creationDate", ascending: false) ...
fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]]; PHFetchResult*> *assets = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:...
NSHipster还探讨了集合操作符、键值观察(KVO)、错误处理(NSError)、NSOperation以及NSSortDescriptor等高级特性。对于那些需要与文件系统交互的开发者,NSFileManager提供了一系列方法来进行文件的创建、删除、移动...
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "localizedTitle", ascending: true)] let smartAlbums = PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .albumRegular, options: ...
fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]]; PHAssetCollection *latestCollection = [PHAssetCollection fetchAssetCollectionsWithType:...
这样的查询语句清晰易读,减少了对NSPredicate和NSSortDescriptor等复杂类的直接操作。 在"MagicalRecord-develop"这个压缩包中,包含了MagicRecord的源码,开发者可以深入研究其内部实现,了解它是如何封装和优化...
02-Queried Color List可能深入到CoreData的查询功能,讲解如何使用NSFetchRequest、NSSortDescriptor和NSPredicate来获取、排序和过滤数据。这在实际应用中非常关键,因为通常我们需要根据用户需求动态地从数据存储...
answer: 使用NSArray类的arrayWithObjects:方法将四个数字组成数组,然后使用NSSortDescriptor类的sortDescriptorWithKey:ascending:方法将数组按照从小到大排序,最后使用NSString类的componentsJoinedByString:...
- **实例**: 创建一个 `NSFetchRequest` 对象来指定查询条件,如 `NSPredicate` 来过滤数据,`NSSortDescriptor` 来排序结果。 #### 三、实操步骤 **1. 创建 CoreData 的 Model** - 打开 Xcode,选择 File > New ...
5. **查询构建器**:Cadmium提供了链式查询接口,允许开发者用更直观的方式构建和执行Core Data查询,减少了对NSPredicate和NSSortDescriptor的依赖。 6. **错误处理**:框架集成了错误处理机制,当出现错误时,...
3. ** fetch请求**:学习如何编写和执行fetch请求,以查询和过滤数据,理解NSFetchRequest、NSSortDescriptor和NSPredicate的使用。 4. **上下文管理**:理解ManagedObjectContext的作用,以及如何在多个线程间安全...
CoreData不直接支持分页查询,但我们可以借助NSSortDescriptor来实现。通过设置排序规则,比如按照时间戳降序排列,每次加载一定数量的最新消息。当用户滚动到顶部时,可以加载更多的历史记录。 此外,为了优化用户...
2. **基于属性的排序**:如果你的数据模型具有特定的属性(如首字母),可以使用`NSSortDescriptor`来根据属性值进行排序。 3. **自定义排序逻辑**:如果需要更复杂的排序规则,可以提供自定义比较函数或比较块。 ...
例如,设置`fetchOptions.sortDescriptors`包含一个`NSSortDescriptor`,指定按创建日期降序排列。 4. **限制获取数量**: 如果像描述中提到的,我们只想获取最近的15张照片,可以在获取结果后进行截取。例如,`...