Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store NSTimeInterval values into a NSMutableArray?

I have a requirement where i have a Video that is played using MPMediaPlayerController. Along with the video i have two buttons where i need to capture the current playback time when the button are clicked and store all the relevant clicks individually. I am able to get the current playback time of the video using "currentPlaybackTime" property which returns NSTimeInterval. But can someone help me in how to store all the NSTimeInterval values into an NSMutableDictionary. I have tried the following ways:

-(void)onClickOfGood {
    NSLog(@"The current playback time in good:%g",moviePlayerController.currentPlaybackTime);
    currentPlaybackTime = moviePlayerController.currentPlaybackTime;
    //NSArray *arrayContainsGoodClicks = [[NSArray alloc]initWithObjects:currentPlaybackTime, nil ];
    NSNumber *goodTimeIntervals = [NSNumber numberWithDouble:currentPlaybackTime];
    NSMutableArray *arrayContainsGoodClicks = [[NSMutableArray alloc]initWithObjects:goodTimeIntervals,nil ];
    NSLog(@"The total count of Array is: %i",[arrayContainsGoodClicks count]);}

But everytime after the click of good button i am getting the Array count as only 1. Can someone please throw a light on where i am going wrong?

like image 777
Pradeep Reddy Kypa Avatar asked Apr 09 '12 15:04

Pradeep Reddy Kypa


2 Answers

But everytime after the click of good button i am getting the Array count as only 1.

This is not surprising, considering that you are creating a brand-new NSMutableArray on the previous line.

To fix this, you need to make NSMutableArray *arrayContainsGoodClicks an instance variable (AKA ivar), initialize it to [NSMutableArray array] in your designated initializer, and then use

[arrayContainsGoodClicks addObject:goodTimeIntervals];

to add objects to the array.

If you are looking to use NSMutableDictionary instead, the strategy would be identical, except you would need to decide on an object that you would like to use as unique keys to your NSDictionary. Also remember that NSMutableDictionary is not ordered, so you might need to take care of sorting each time you display your dictionary items to users.

like image 133
Sergey Kalinichenko Avatar answered Sep 21 '22 02:09

Sergey Kalinichenko


You need to create arrayContainsGoodClicks only once (in init method for example) and then add value to this array in your button handler:

//.h
NSMutableArray *arrayContainsGoodClicks;

//.m - init
arrayContainsGoodClicks = [NSMutableArray array];

//.m - button handler
[arrayContainsGoodClicks addObject:goodTimeIntervals];
like image 28
beryllium Avatar answered Sep 20 '22 02:09

beryllium