Friday, June 21, 2013

NSMutableIndexSet addIndexes example in Objective C (iOS).


NSMutableIndexSet addIndexes

Adds the indexes in an index set to the receiver.

- (void)addIndexes:(NSIndexSet *)indexSet

Parameters of [NSMutableIndexSet addIndexes]
indexSet
Index set to add.

NSMutableIndexSet addIndexes example.
Because, you work only with integer, using a NSMutableIndexSet would be much more efficient.

#import "NumberCreator.h"

@implementation NumberCreator
@synthesize levels, numbers;

- (id)init
{
    if ( !(self = [super init] ) )
        return nil;

    numbers = [NSMutableIndexSet new];
    return self;
}

- (void)create
{
    [numbers removeAllIndexes];
    [numbers addIndex:1];

    NSMutableIndexSet *nextLevel = [NSMutableIndexSet indexSet];
    NSMutableIndexSet *currentLevel = [NSMutableIndexSet indexSetWithIndex:1];

    for (int i = 0; i < levels; i++ )
    {
        [currentLevel enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {

            if ( ! [numbers containsIndex:idx * 2] )
                [nextLevel addIndex:idx * 2];

            if ( idx % 6 == 4 && ! [numbers containsIndex:(idx - 1) / 3] )
                [nextLevel addIndex:(idx - 1) / 3];
        }];

        [numbers addIndexes:nextLevel];
        [currentLevel removeAllIndexes];
        [currentLevel addIndexes:nextLevel];
        [nextLevel removeAllIndexes];
    }

    [numbers enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
        NSLog(@"%i", idx);
    }];
}

@end

Example of [NSMutableIndexSet addIndexes].
Have you tried this:

 [someIndexSet addIndexes:otherIndexSet]

NSMutableIndexSet addIndexes example.
RKObjectManager *manager = [RKObjectManager sharedManager];
    RKObjectMapping *map = [RKObjectMapping mappingForClass:[DataError class]];
    RKObjectMapping *sub =  [RKObjectMapping mappingForClass:[DataErrorDetail class]];
    [sub addAttributeMappingsFromDictionary: @{
     @"key": @"code",
     @"value": @"description",
     }];
    [map addAttributeMappingsFromDictionary: @{
     @"error": @"error",
     @"message": @"message"
     }];
   
    [map addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"reasons" toKeyPath:@"reasons" withMapping:sub]];
   
    NSMutableIndexSet *codes = [[NSMutableIndexSet alloc] init];
    [codes addIndexes:RKStatusCodeIndexSetForClass(RKStatusCodeClassClientError)];
    [codes addIndexes:RKStatusCodeIndexSetForClass(RKStatusCodeClassServerError)];
   
    [manager addResponseDescriptor:
     [RKResponseDescriptor responseDescriptorWithMapping:map pathPattern:nil keyPath:@"error" statusCodes:codes]];

End of NSMutableIndexSet addIndexes example article.