问题描述
我正在尝试根据字母在表视图中创建节,并在这些节下按字母顺序对条目进行排序.
I'm trying to make sections in a table view based on the alphabet and sort my entries alphabetically under these sections.
我已经在bandsArrayIndex中的bandsArray的每个条目中收集了第一个字母,现在我正尝试使用NSPredicate来计算每个字母中有多少个.
I have collected the first letter in every entry of bandsArray in bandsArrayIndex and I'm now trying to use NSPredicate to count how many of each letter there are.
我正在按照本指南进行操作.
他们正在使用NSArray,而我正在使用NSDictionary,似乎无法使其正常工作.有人可以帮我吗?
They are using an NSArray and I'm using an NSDictionary and can't seem to get it to work. Can anyone help me out?
尝试在其中显示带有表格视图的视图时,应用程序崩溃.在调试器控制台中,显示以下错误:
The application crashes when trying to show the view with the table view in it.In Debugger Console the following error is shown:
这是我的代码
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSString *alphabet = [bandsArrayIndex objectAtIndex:section];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", alphabet];
NSArray *letters = [bandsArray filteredArrayUsingPredicate:predicate];
return [letters count];
}
这是我的bandsArray.
This is my bandsArray.
标题
NSMutableArray *bandsArray;
@property (nonatomic, retain) NSMutableArray* bandsArray;
实施
// set array from plist
NSString *path = [[NSBundle mainBundle] pathForResource:@"Bands" ofType:@"plist"];
NSMutableArray* tmpArray = [[NSMutableArray alloc] initWithContentsOfFile:path];
self.bandsArray = tmpArray;
// sort array after name ascending
NSSortDescriptor *nameSorter = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES selector:@selector(caseInsensitiveCompare:)];
[bandsArray sortUsingDescriptors:[NSArray arrayWithObject:nameSorter]];
推荐答案
您是说bandsArray
是一个字典数组吗?如果是这样,并假设每个词典都有一个name
键,则您应该可以将谓词更改为类似@"SELF.name beginswith[c] %@"
的谓词.
Do you mean that bandsArray
is an array of dictionaries? If so, and assuming each dictionary has a name
key, you should be able to change the predicate to something like @"SELF.name beginswith[c] %@"
.
反之,如果bandsArray
实际上是字典本身,那么也许您想做[[bandsArray allKeys] filteredArrayUsingPredicate...]
.
If, on the other hand, bandsArray
is actually a dictionary itself, maybe you want to do [[bandsArray allKeys] filteredArrayUsingPredicate...]
.
这篇关于NSPredicate在NSDictionary的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!