Saturday, June 1, 2013

NSMutableArray exchangeObjectAtIndex example in Objective C (iOS).


NSMutableArray exchangeObjectAtIndex

Exchanges the objects in the array at given indices.

- (void)exchangeObjectAtIndex:(NSUInteger)idx1 withObjectAtIndex:(NSUInteger)idx2

Parameters of [NSMutableArray exchangeObjectAtIndex]
idx1
The index of the object with which to replace the object at index idx2.
idx2
The index of the object with which to replace the object at index idx1.

NSMutableArray exchangeObjectAtIndex example.
// get the shared defaults object
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

// remove any previous setting for languages
[userDefaults removeObjectForKey:@"AppleLanguages"];

// lookup the current array of languages
// Note: if you think this returns null because of the line above, you should read the docs on NSUserDefault domains! ;-)
NSArray *languages = (NSArray *)[userDefaults objectForKey:@"AppleLanguages"];

// filter the list of languages to include only ones which you support
// give the user a UI to select the language which they would like
// change the language array so that the selected language is the first entry
//
// for my example, I swap the first and second languages (result depends on your setup but you get the idea.)
NSMutableArray *mutableLanguages = [languages mutableCopy];
[mutableLanguages exchangeObjectAtIndex:0 withObjectAtIndex: 1];

// save the updated languages
[userDefaults setObject:mutableLanguages forKey:@"AppleLanguages"];


Example of [NSMutableArray exchangeObjectAtIndex].
-(void)arrayShuffle: (NSMutableArray *) a {
   nsm = [[NSMutableArray alloc] initWithArray: a];
    while ([nsm count] > 1 ) {
        // x - 1 will be the highest randomly generated number to match with the highest index of the array
        int x = ([nsm count] - 1);
        int randValue1 = arc4random() % x;
        [nsm exchangeObjectAtIndex: 0 withObjectAtIndex: randValue1];  //exchanges objectAtIndex: 0 with index from randomly generated number
       NSLog(@"%@", [nsm objectAtIndex: randValue1]);
        [nsm removeObjectAtIndex: randValue1];  //removes the used object: this would be a question in the quiz.
       
        if ([nsm count] == 1) {  //since [nsm count] == 0 didn't work w/ arc4random, this takes care of the remaining object in the array.
            NSLog(@"%@", [nsm objectAtIndex: 0]);
        }
    }
}

NSMutableArray exchangeObjectAtIndex example.
Code

//  NSMutableArray_Shuffling.m



#import "NSMutableArray_Shuffling.h"





@implementation CategoryDummy

@end





@implementation NSMutableArray (Shuffling)



- (void)shuffle

{

    NSUInteger count = [self count];

    for (NSUInteger i = 0; i < count; ++i) {

        // Select a random element between i and end of array to swap with.

        int nElements = count - i;

        int n = (arc4random() % nElements) + i;

        [self exchangeObjectAtIndex:i withObjectAtIndex:n];

    }

}



@end

End of NSMutableArray exchangeObjectAtIndex example article.