Search This Blog

Friday, 20 December 2013

How to Shuffle the Array

Hello friends!

Here is the very simple and easy example to shuffle array  values
here we go!
Just copy and paste in your .m file

 >>randomNo is NSMutableArray
>>ran is int value
>>
//random shuffle generate start


static NSUInteger random_below(NSUInteger n)
{
    int ran;
    ran = 1+ arc4random() % 5;
    //NSLog(@"random>>>%i",ran);
    NSUInteger m = ran;
    
    // Compute smallest power of two greater than n.
    // There's probably a faster solution than this loop, but bit-twiddling
    // isn't my specialty.
    do {
        m <<= 1;
    } while(m < n);
    
    NSUInteger ret;
    
    do {
        ret = random() % m;
    } while(ret >= n);
    
    return ret;
}


- (void)shuffle {
    for(NSUInteger i = [randomNo count]; i > 1; i--) {
        NSUInteger j = random_below(i);
        [randomNo exchangeObjectAtIndex:i-1 withObjectAtIndex:j];
    }
    NSLog(@"this is shuffle array %@",randomNo);

}


//random shuffle  generat end


iOS NSNotification

NSNotification is a great and simple way to inform other 
methods to trigger in different classes
eg : we have two classes : class A and class B , in class B 
we perform some tasks and we have to inform class A that 
class B has finished it's task so, we can handle this by
simple implementation of two piece of code : one is post 
notification which has to implement in class B and other one
will be implemented in class A which will receive notification from class 

Class B:
Below method you can implement after completing any task with any
 key liek ("Task_done")

    [[NSNotificationCenter defaultCenter]
     postNotificationName:@"Task_done"

     object:self];

Class A:
This class will receive notification with the same key , and it
 will automatically call the method (AfterTaskDone:)

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(AfterTaskDone:)

                                                 name:@"Task_done"        object:nil];

-(void)AfterTaskDone{
    

}