I’m very new to Objective-C and need a little help. I have an array of songs:
{
artist = "Bernard Herrmann";
duration = "2:34";
id = 5;
title = "Torn Curtain: The Unused Score";
},
{
artist = "Miklos Razsa";
duration = "3:47";
id = 6;
title = "Ivanhoe [Original]";
},
...etc.
And I’d like to iterate through the array, converting the durations(currently NSStrings) to something I can use to represent the actual elapsed time, then add them up so that I can get a count of total time to play the playlist. I’ve started working on the loop:
UPDATED:
- (NSInteger)convertDuration
{
NSInteger totalSeconds=0;
for(NSMutableArray *song in playlist){
NSString *durationString = ;
NSArray *breakMinSec = [durationString componentsSeparatedByString:@":"];
NSInteger sec = [breakMinSec[1]integerValue];
NSInteger min = [breakMinSec[0]integerValue];
totalSeconds += ( sec + min * 60 );
}
return totalSeconds;
}
It looks like as of right now, durationString is pulling all of the durations in the first iteration. It won’t compile like this. How can I be sure only to get a single duration for each iteration?
Here no need to convert it into NSTimeInterval or NSDate. You can simply add all durations to an array. Then break minutes and seconds, add all minutes and all seconds.
Then
Edit: An idea how you can do (not compiler checked)
This will store each songs time in seconds. then you can manupulate this value as per your need.
EDIT:
After seeing your edit:
You can send a value to your method that will return the duration for that specific song. Or as in my earlier answer, each duration was saved in index based array, you could have easily retrieved from there.