objective-c - 如何将带有变量的 NSString 分配给 nsstring 对象?

标签 objective-c c

我有以下代码:

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

最佳答案

您需要使用stringWithFormat,参见NSString Class Reference .

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

关于objective-c - 如何将带有变量的 NSString 分配给 nsstring 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11420100/

相关文章:

iOS objective-c : how to set and ImageView from an URL

objective-c - 如何安装和使用 iOS 6 中包含的附加亚洲字体?

c - C中的数组字符串和普通数组有什么区别?

iphone - 如何在分段控件中获取所选分段的标题?

iphone - 为什么这个条件语句不能与 NSUserDefault 一起使用?

objective-c - 如何在Mac上与其他用户共享iPhone应用程序?

c - 传递 'unpack_code' 参数 3 时的指针目标符号性不同 [-Wpointer-sign],如何解决这个问题?

c - av_write_header - 样本格式错误

c - 我的程序跳过获取输入数据?

c - 多线程对速度没有改进 - 在 C 中使用 pthread - 为什么?