Wednesday, 22 January 2014

Swap array objects

//When clicking button it change different
- (IBAction)click:(id)sender {
    /*[ar replaceObjectAtIndex:0 withObject:[ar objectAtIndex:2]];
    [ar replaceObjectAtIndex:1 withObject:[ar objectAtIndex:0]];
    [ar replaceObjectAtIndex:2 withObject:[ar objectAtIndex:1]];
    */
    NSUInteger count = [ar count];
    for (NSUInteger i = 0; i < count; ++i) {
        int nElements = count - i;
        int n = (arc4random() % nElements) + i;
        [ar exchangeObjectAtIndex:i withObjectAtIndex:n];
        
    }
    t1.text=[ar objectAtIndex:0];
    t2.text=[ar objectAtIndex:1];
    t3.text=[ar objectAtIndex:2];

}

No comments:

Post a Comment