In xcode, I have a database that I request from an online sql database and this can be stored into an array or NSDictionary or other type. Separately, an array of numbers is generated in the app which correspond to one of the columns of the database.
在xcode中,我有一个数据库,我从在线sql数据库请求,这可以存储到数组或NSDictionary或其他类型。另外,在app中生成一个数字数组,它们对应于数据库的一列。
I want to filter the database to only show objects involved with generated array. Once filtered, I want to list the results in a string. The database from the online server can be stored in an NSArray or an NSDictionary if need be, and probably other formats that I don't know about.
我想过滤数据库,只显示与生成的数组有关的对象。过滤后,我想在字符串中列出结果。如果需要,可以将在线服务器中的数据库存储在NSArray或NSDictionary中,也可以存储我不了解的其他格式。
The procedure would look something like this:
该过程看起来像这样:
Database array: Generated Array: Resultant array after filtering the database:
数据库数组:生成的数组:过滤数据库后的结果数组:
ID | Name ID FilteredNames
|
1A | Hannah 1A Hannah
2A | John 1B Steve
3A | Peter 2B Zara
1B | Steve
2B | Zara
3B | Kyle
The the resultant array "FilteredNames" converted to a String list as follows:
结果数组“FilteredNames”转换为String列表,如下所示:
NamesString = @"Hannah, Steve, Zara"
I then plan to pass the NamesString to a label as follows:
然后我计划将NamesString传递给标签,如下所示:
label.text=NamesString;
and so the label in the view just shows :
所以视图中的标签只显示:
Hannah, Steve, Zara
I pretty much need help for this entire procedure.
我非常需要整个程序的帮助。
1 个解决方案
#1
1
EDIT Following Martin's comment, changed the predicate format to a much more appropriate one.
编辑按照Martin的评论,将谓词格式更改为更合适的格式。
- (NSArray *)filterObjects:(NSArray *)objects withNames:(NSArray *)names {
return [objects filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"name IN %@", names]];
}
// given that the 'objects' array is what you get from server
NSArray *objects = @[
@{@"id" : @"1A", @"name" : @"Hannah"},
@{@"id" : @"2A", @"name" : @"John"},
@{@"id" : @"3A", @"name" : @"Peter"},
@{@"id" : @"1B", @"name" : @"Steve"},
@{@"id" : @"2B", @"name" : @"Zara"},
@{@"id" : @"3B", @"name" : @"Kyle"}
];
NSArray *filteredObjects = [self filterObjects:objects withNames:@[@"Hannah", @"John", @"Zara"]];
NSLog(@"filteredObjects: %@", filteredObjects); // prints them out as dictionaries
NSString *unitedNames = [[filteredObjects valueForKeyPath:@"@unionOfObjects.name"] componentsJoinedByString:@", "];
NSLog(@"unitedNames: %@", unitedNames); // comma separated array of names
#1
1
EDIT Following Martin's comment, changed the predicate format to a much more appropriate one.
编辑按照Martin的评论,将谓词格式更改为更合适的格式。
- (NSArray *)filterObjects:(NSArray *)objects withNames:(NSArray *)names {
return [objects filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"name IN %@", names]];
}
// given that the 'objects' array is what you get from server
NSArray *objects = @[
@{@"id" : @"1A", @"name" : @"Hannah"},
@{@"id" : @"2A", @"name" : @"John"},
@{@"id" : @"3A", @"name" : @"Peter"},
@{@"id" : @"1B", @"name" : @"Steve"},
@{@"id" : @"2B", @"name" : @"Zara"},
@{@"id" : @"3B", @"name" : @"Kyle"}
];
NSArray *filteredObjects = [self filterObjects:objects withNames:@[@"Hannah", @"John", @"Zara"]];
NSLog(@"filteredObjects: %@", filteredObjects); // prints them out as dictionaries
NSString *unitedNames = [[filteredObjects valueForKeyPath:@"@unionOfObjects.name"] componentsJoinedByString:@", "];
NSLog(@"unitedNames: %@", unitedNames); // comma separated array of names