gpt4 book ai didi

ios - 从数组中删除项目(带有来自 API 的 JSON 的 Restkit 项目)

转载 作者:行者123 更新时间:2023-11-28 20:00:59 25 4
gpt4 key购买 nike

我有一个叶子数组,我想删除数组中的一些对象。

数组中大约有 50 个对象,我只想要数组中的大约 10 个;数组中需要的 10 个对象混合在数组中的 50 个对象中。

我在我的项目中使用 RestKit,并将 leafs 放入 TableView 中。

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;
}

Spring.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;

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];
}

成功 block

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];

最佳答案

如果您想在映射期间过滤掉项目,请查看使用 RestKit 的 KVC 验证。

如果您希望所有项目都在设备上可用并且只过滤显示,请使用谓词过滤数组。

关于ios - 从数组中删除项目(带有来自 API 的 JSON 的 Restkit 项目),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24072117/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com