我有个数组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;
}
春季
@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;
Per 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;
}
}
Per 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];
如果要在映射过程中过滤掉项目,请考虑将KVC验证与RestKit一起使用。
如果要使所有项目在设备上可用并且仅过滤以显示,请使用谓词过滤阵列。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句