をintに私は、iPhone用のアプリで作業し、送信する必要があるんだそう優しくしてください...初心者Objective Cの関数のNSStringの
、Visual Basicの背景(昔)から来ますNSString(NSStringを整数に変換して値を返す関数のNSStringだと思いますが、コアデータの属性が整数なので、おそらく整数値として返されるはずです)。
私は必要ですUILabelにソングの時間を表示するには、NSSTringです。コアデータの属性にデータを整数として保存します。計算を簡単にするなど、私がやっていることの1つです秒(int)を "3:32"に変換しています。NSStringラベルのために。
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];
}
もちろん、このコードにはいくつかの問題がありますが、間違った点や修正方法を簡単に教えていただきたいと思います。事前に
おかげ
-Paul
改訂ソリューション:
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];
}
は解決する唯一の残りの項目はlengthOfSongInmmssの場合です。まれに私は1時間の歌しか持たないでしょうが、それは "ちょうどその場"のためだけです。文字列形式で時間を表示しない方法がありますか、それとも単純な "if"文?
もう一度おねがいします!
'[managedObject valueForKey:]'はintではなく '(id)'を返します。OPは '[(NSNumber *)[managedObject valueForKey:@" profileItemsSongDurationInSeconds "] intValue]' – commanda
を実行する必要があります。 –