You can use the following piece of code to randomize/shuffle the objects inside a NSMutableArray.
/* anArray is a NSMutableArray with some objects */
srandom(time(NULL));
NSUInteger count = [anArray count];
for (NSUInteger i = 0; i < count; ++i) {
int nElements = count - i;
int n = (random() % nElements) + i;
[anArray exchangeObjectAtIndex:i withObjectAtIndex:n];
}
/* anArray is a NSMutableArray with some objects */
NSUInteger count = [anArray count];
for (NSUInteger i = 0; i < count; ++i) {
int nElements = count - i;
int n = (arc4random() % nElements) + i;
[anArray exchangeObjectAtIndex:i withObjectAtIndex:n];
}
You can get the Date and Time in NSString using NSDateFormatter.
// Get current datetime
NSDate *currentDateTime = [NSDate date];
// Instantiate a NSDateFormatter
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// Set the dateFormatter format
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
// Get the date time in NSString
NSString *dateInString = [dateFormatter stringFromDate:currentDateTime];
// Release the dateFormatter
[dateFormatter release];
Unlike Android, iPhone allows users setting their preferred language code in iPhone. If you want to get the preferred language, the following piece of codes should help.
I am working for my next iPhone application which will create an audio file and save it in the Documents directory. I also created a table view which listed all the audio files such that user can delete them. Please note that the Documents directory is the only place where you can store your application files. Continue reading iPhone – Delete Files in the Documents Directory of your iPhone Application→
Update @ 2012-08-02: If you are using XCode 4.3 or above. You could following the instruction provided by S.Littlehales.
I found that if u set an image to the UIBarButtomItem directly, only the shape of the image is shown in the view without any color. If you want to add an button image to UIBarButtonItem, you have to create a UIButton and set it to the view of the UIBarButtonItem. Continue reading iPhone – Adding Image to UIBarButtonItem→
Yesterday we talk about how to make use of the NSTimer for scheduling a task. iPhone – NSTimer Example
Sometimes, you may want to stop the scheduled task. Then you can use the invalidate method. If you invalidate a unfired NSTimer, that’s fine. But if you invalidate a fired NSTimer, the application will crashed. So how to by pass the problem? Continue reading iPhone – Stop the Scheduled NSTimer→