2012-02-17 3 views
0

を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"文?

もう一度おねがいします!

答えて

2

そして、このようなメソッド内で整数を直接使用してください:

int hours = songLength/3600; 
+1

'[managedObject valueForKey:]'はintではなく '(id)'を返します。OPは '[(NSNumber *)[managedObject valueForKey:@" profileItemsSongDurationInSeconds "] intValue]' – commanda

+0

を実行する必要があります。 –

0

NSStringカテゴリを作成し、その回避策を実行する必要があります。

ただし、その関数を何回使用する必要があるかによって、カテゴリなどを作成する代わりに情報が必要なメソッドに直接実装するほうが簡単になる場合があります。

2

変換方法を正しく呼び出す必要はありません。あなたは、Objective-Cメソッド呼び出し構文を使って、C関数呼び出し構文をブレンドしているのです。ここでは、あなたの関数を呼び出すための正しい方法です:

cell.profileItemsSongDurationLabel.text = [self ConvertSecondstoMMSS:[[managedObject valueForKey:@"profileItemsSongDurationInSeconds"] description]; 

はConvertSecondstoMMSSは、その行と同じクラスであると仮定すると。

また、あなたのメソッド名を大文字で入力する必要があります。名前をconvertSecondsToMMSSに変更します。

NSManagedObjectは整数を返すので、これを行う:

-(NSString *)ConvertSecondstoMMSS:(int)songLength 

:そして

cell.profileItemsSongDurationLabel.text = [self ConvertSecondstoMMSS:[managedObject valueForKey:@"profileItemsSongDurationInSeconds"]]; 

を同様に整数を期待するメソッドの宣言を変更するエラーに対処するため

+0

お返事ありがとうございます。 –