Tag Archives: Objective-C

Objective-C – How to Shuffle a NSMutableArray

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];
}

 

Update @ 2012-07-18: Suggestion from kimbebot

/* 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];
}

Done =)

Reference: StackOverflow – What’s the Best Way to Shuffle an NSMutableArray?

Objective-C – Display Date and Time Using NSDateFormatter

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];

Continue reading Objective-C – Display Date and Time Using NSDateFormatter

iPhone – Delete Files in the Documents Directory of your iPhone Application

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

iPhone – Adding Image to UIBarButtonItem

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

iPhone – Add an UISlider For AVAudioPlayer

We can use AVAudioPlayer to play music. Now i want to add an UISlider as the music progress bar and user can fast skip the music.

Now i have a play button in the view and when user click it, i will play the music file as well as the following method.

  • playButtonClickedRun when the play button is clicked
  • updateSliderRun in 1 second interval to update the UISlider
  • sliderChangeFast skip the music when user scroll the UISlider

Continue reading iPhone – Add an UISlider For AVAudioPlayer

iPhone – Stop the Scheduled NSTimer

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