【发布时间】:2014-06-05 23:58:37
【问题描述】:
我有一个leafs 数组,我想删除数组中的一些对象。
数组中有大约 50 个对象,我只想要数组中的大约 10 个;数组中需要的 10 个对象混合在数组中的 50 个对象中。
我在我的项目中使用 RestKit,并将 leafs 放入表视图中。
ViewController.m
@property (strong, nonatomic) NSArray *springs;
@property (strong, nonatomic) NSMutableArray *leafs;
@end
@synthesize tableView=_tableView;
@synthesize springs;
@synthesize leafs;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self configureRestKit];
[self loadLeafs];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)configureRestKit {
// initialize AFNetworking HTTPClient
NSURL *baseURL = [NSURL URLWithString:@"https://api.e.com"];
AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:baseURL];
// initialize RestKit
RKObjectManager *objectManager = [[RKObjectManager alloc] initWithHTTPClient:client];
// setup object mappings
RKObjectMapping *springMapping = [RKObjectMapping mappingForClass:[Spring class]];
[springMapping addAttributeMappingsFromArray:@[@"name"]];
RKObjectMapping *leafMapping = [RKObjectMapping mappingForClass:[Leaf class]];
[leafMapping addAttributeMappingsFromArray:@[@"abbreviation", @"shortName", @"id"]];
[springMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"leafs" toKeyPath:@"leafs" withMapping:leafMapping]];
// Wain, this is where I'm getting that error:
// "property discardsinvalidobjectsoninsert not found on object of type RKObjectMapping"
springMapping.discardsInvalidObjectsOnInsert = YES;
// register mappings with the provider using a response descriptor
RKResponseDescriptor *responseDescriptor =
[RKResponseDescriptor responseDescriptorWithMapping:springMapping
method:RKRequestMethodGET
pathPattern:nil
keyPath:@"springs"
statusCodes:[NSIndexSet indexSetWithIndex:200]];
[objectManager addResponseDescriptor:responseDescriptor];
}
- (void)loadLeafs {
NSString *apikey = @kCLIENTKEY;
NSDictionary *queryParams = @{@"apikey" : apikey,};
[[RKObjectManager sharedManager] getObjectsAtPath:@"v1/springs/"
parameters:queryParams
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
springs = mappingResult.array;
[self.tableView reloadData];
}
failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(@"No springs?': %@", error);
}];
}
#pragma mark - Table View
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return springs.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
Spring *spring = [springs objectAtIndex:section];
return spring.leafs.count;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
Spring *spring = [springs objectAtIndex:section];
return spring.name;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"standardCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
Spring *spring = [spring objectAtIndex:indexPath.section];
Leaf *leaf = [spring.leafs objectAtIndex:indexPath.row];
cell.textLabel.text = leaf.shortName;
return cell;
}
春天.h
@property (nonatomic) NSString *name;
@property (nonatomic) NSNumber *id;
@property (nonatomic) NSArray *leafs;
叶子.h
@property (nonatomic) NSString *name;
@property (nonatomic) NSString *abbreviation;
@property (nonatomic) NSString *shortName;
@property (nonatomic) NSNumber *id;
每个 Wain,我在 Spring.h 中添加了这个
@property (nonatomic, assign) BOOL discardsInvalidObjectsOnInsert;
Per Wain,我在 Spring.m 中添加了这种东西
- (BOOL)validateName:(id *)ioValue error:(NSError **)outError {
if([(NSString *)*ioValue isEqualToString:@"cricket"]){
NSLog(@"old name: %@, new name: %@", self.name, *ioValue);
// Wain: set the names to nil for the objects you don't want
ioValue = nil;
return NO;
// Wain: should I keep this line?
self.discardsInvalidObjectsOnInsert = YES;
}
else{
return YES;
}
}
每个 Wain,我在 ViewController.m 中添加了这个
- (void)loadLeafs {
NSString *apikey = @kCLIENTKEY;
NSDictionary *queryParams = @{@"apikey" : apikey,};
[[RKObjectManager sharedManager] getObjectsAtPath:@"v1/springs/"
parameters:queryParams
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
springs = mappingResult.array;
[self.tableView reloadData];
}
failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(@"No springs?': %@", error);
}];
// Wain: filter (using a predicate) the mapping array (name != nil)
NSPredicate *bPredicate = [NSPredicate predicateWithValue:NO];
self.filteredArray = [self.sports filteredArrayUsingPredicate:bPredicate];
}
成功阻止
NSPredicate *filterPredicate = [NSPredicate predicateWithFormat:@"name != nil"];
springs = [mappingResult.array filteredArrayUsingPredicate:filterPredicate];
// You need to loop over the springs after you filter them and then filter the leafs of each spring
for (Spring *spring in springs) {
for (Leaf *leaf in spring.leafs){
NSPredicate *filterPredicate2 = [NSPredicate predicateWithFormat:@"shortName != nil"];
leafs = [spring.leafs filteredArrayUsingPredicate:filterPredicate2];
}
}
[self.tableView reloadData];
【问题讨论】:
-
创建一个新的 NSMutableArray,遍历你的数组,将你想要的添加到新数组中?
-
您使用什么标准来确定是否要移除叶子?
-
@Mike 好问题,我也会在我原来的问题中说明这一点。我想指定
leafs要包含的shortNames。比如leafs数组中,我只想要shortNames:aaa, bbb, ccc, ddd, eee。这样说清楚了吗? -
@jraede 感谢您的回复!你有如何做到这一点的链接,或者你可以发布一个sn-p代码吗?我也有点理解我需要尝试做的事情的总体思路,但具体的实现是我在这里做的不足之处?