0

我有以下代码:

long mins = 02; long secs = 35;
  NSString *allTime = [[NSString alloc]init];
    allTime = @"%i:%i",mins, secs ;

但它不起作用,因为当我尝试显示那个 nsstring 对象时,我得到了这个:%i:%i 而不是我想要得到的:02:35

怎么做 ?谢谢!

allTime = [NSString stringWithFormat:@"%l/%l", mins, secs];
for(Playlists *thePL in collection)
    {
        NSLog(@"===NEXT PLAYLIST===");
        NSLog(@"Name: %@", thePL.namePL);
        NSLog(@"Quantity of songs: %i", thePL.entries);
        NSLog(@"Total of time: %@",thePL.allTime);
    }

总时间: l

4

4 回答 4

6

您需要使用stringWithFormat,请参阅NSString Class Reference

allTime = [NSString stringWithFormat:@"%d:%d",mins, secs];
于 2012-07-10T19:03:27.990 回答
4
long mins = 02;
long secs = 35;   
NSString *allTime = [[NSString alloc] initWithFormat: @"%d:%d", mins, secs]; 
于 2012-07-10T19:04:20.080 回答
3

尝试:

NSString *allTime = [NSString stringWithFormat:@"%d:%d",mins, secs];
于 2012-07-10T19:05:36.613 回答
0

首先你应该阅读 NSString 的 calss 参考。其次,您应该使用以下代码:

 NSString *allTime = [stringWithFormat:@"%d,:%d", mins, secs]

您还可以分配字符串,然后通过这种方式对其进行初始化:

NSString *allTime = [[Nsstring alloc] init];
allTime = [NSString stringWithForat:@"%d:%d", mins, secs];

虽然,最好的,短的和更好的方法是第一个。希望这有帮助...

于 2012-07-10T19:44:07.060 回答