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

Realinstomp

我有个数组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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用jquery从JSON删除数组中的所有项目

来自分类Dev

在laravel中删除带有模态的项目

来自分类Dev

从数组中删除项目

来自分类Dev

从数组中删除项目

来自分类Dev

删除并返回数组中的所有项目

来自分类Dev

从具有嵌套项目的数组中删除项目

来自分类Dev

vuejs从数组中删除项目

来自分类Dev

从状态数组中删除项目

来自分类Dev

从数组中删除特定项目

来自分类Dev

如何从数组中删除项目

来自分类Dev

从数组结果中删除项目

来自分类Dev

从角度材料列表中删除带有标题的项目

来自分类Dev

拼接或删除JSON数组中的元素列表,仅删除偶数编号的项目,而不是匹配的项目

来自分类Dev

从数组中删除不具有相等性的项目

来自分类Dev

从数组中删除不具有相等性的项目

来自分类Dev

从REST API中获取列表数据后,希望从项目数组中删除列表项目

来自分类Dev

KnockoutJS:从数组中删除项目数组

来自分类Dev

KnockoutJS:从数组中删除项目数组

来自分类Dev

从CouchDB中的数组中删除项目

来自分类Dev

从Javascript Titan中的数组中删除项目

来自分类Dev

从PHP中的多维数组中删除项目

来自分类Dev

从Vuex中的数组中删除特定项目

来自分类Dev

无法从React中的数组中删除项目

来自分类Dev

在React中无法从数组中删除项目

来自分类Dev

从JavaScript中的数组中删除项目

来自分类Dev

在React中从状态数组中删除项目

来自分类Dev

从Javascript Titan中的数组中删除项目

来自分类Dev

如何删除数组中的项目?

来自分类Dev

从数组中删除项目而不返回它