Note that there are some explanatory texts on larger screens.

plurals
  1. PONewbie Objective C function's NSString to Int
    primarykey
    data
    text
    <p>I come from a Visual Basic background (a long time ago), so be gentle please...</p> <p>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.</p> <p>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. </p> <pre><code>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]; } </code></pre> <p>Naturally this code has some issues, but I would appreciate a quick lesson on what's wrong, and how to fix it.</p> <p>Thanks in advance</p> <p>-Paul</p> <p>Revised solution:</p> <pre><code>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]; } </code></pre> <p>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?</p> <p>Thanks again!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload