I come from a Visual Basic background (a long time ago), so be gentle please…
I’m working on an app for the iPhone, and need to send an NSString (I believe it’s an NSString to a function that is going to convert the NSString to an integer and return the value. The value should probably be returned as an integer as the Core Data attribute is an integer.
I need to display the time of a song in a UILabel, and it’s an NSSTring. I’m storing the data in the Core Data attribute as an integer, as it’s easier to do calculations on it, etc. One of the things I’m doing is converting the seconds (int) into “3:32” NSString for the Label.
cell.profileItemsSongDurationLabel.text= ConvertSecondstoMMSS([[managedObject valueForKey:@"profileItemsSongDurationInSeconds"] description]);
// implicit declaration of function 'ConvertSecondstoMMSS' is invalid in C99
// Implicit conversion of 'int' to 'NSString *' is disallowed with ARC
// Incompatible integer to pointer conversion passing 'int' to parameter of type 'NSString *'
- (NSString *)ConvertSecondstoMMSS:(NSString *)songLength // Conflicting return type in implementation of 'ConvertSecondstoMMSS:': 'int' vs 'NSString *'
{
NSString *lengthOfSongInmmss;
int songLengthInSeconds = [songLength intValue];
int hours = songLengthInSeconds/ 3600;
int minutes = (songLengthInSeconds % 3600) / 60;
int seconds = songLengthInSeconds % 60;
return lengthOfSongInmmss = [NSString stringWithFormat:@"%d:%02d:%02d", hours, minutes, seconds];
}
Naturally this code has some issues, but I would appreciate a quick lesson on what’s wrong, and how to fix it.
Thanks in advance
-Paul
Revised solution:
int convertToInt = [[[managedObject valueForKey:@"profileItemsSongDurationInSeconds"] description] intValue];
NSLog(@"convertToInt: %d", convertToInt);
cell.profileItemsSongDurationLabel.text = [self convertSecondstoMMSS:convertToInt];
- (NSString *)convertSecondstoMMSS:(int)songLength
{
NSString *lengthOfSongInmmss;
int hours = songLength/ 3600;
int minutes = (songLength % 3600) / 60;
int seconds = songLength % 60;
return lengthOfSongInmmss = [NSString stringWithFormat:@"%d:%02d:%02d", hours, minutes, seconds];
}
The only remaining item to resolve is the case of the lengthOfSongInmmss. Rarely will I have a song with a duration of an hour, it’s only there for the “just in case” Is there a way to not have the hour shown via the string format, or it it better to do a simple “if” statement?
Thanks again!
To address the errors:
Since the
NSManagedObjectis returning an integer, do this:Then change the method declaration to expect an integer as in:
And use the integer directly within the method like this: