标签: stringwithformat

如何设置显示数字的填充字符(目标C)

我正在尝试格式化一个字符串,为一个数字提供2个字符,无论它的值是什么.现在,我有

[NSString stringWithFormat:@"%2d:%2d:%2d",h,m,s)];
Run Code Online (Sandbox Code Playgroud)

对于值1,2,3,输出为

 1: 2: 3
Run Code Online (Sandbox Code Playgroud)

如何将空格更改为0?

iphone cocoa printf stringwithformat

2
推荐指数
1
解决办法
834
查看次数

使用stringWithFormat:作为cocoa中的文件路径

我遇到了一个带有文本字段值的cocoa应用程序的问题,并将其写入文件.使用stringWithFormat:创建文件路径以组合2个字符串.由于某种原因,它不会创建文件,控制台什么都不说.这是我的代码:

//Get the values of the text field
NSString *fileName = [fileNameTextField stringValue];
NSString *username = [usernameTextField stringValue];

//Use stringWithFormat: to create the file path
NSString *filePath = [NSString stringWithFormat:@"~/Library/Application Support/Test/%@.txt", fileName];

//Write the username to filePath
[username writeToFile:filePath atomically:YES];
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助

string cocoa text-files stringwithformat

1
推荐指数
1
解决办法
1619
查看次数

如何使用stringWithFormat动态地将连字符添加到我的字符串中

嗨,只是想知道是否可能,因为用户正在输入一串数字,以便每隔字符串第5个字符动态添加一个连字符...

任何帮助将不胜感激.

iphone hyphenation nsstring stringwithformat ios

1
推荐指数
1
解决办法
1353
查看次数

将NSInteger转换为NSString作为5位数字

我有一个int,我将它转换为NSString,如下所示:

NSString *myStr = [NSString stringWithFormat:@"%d",myInt];
Run Code Online (Sandbox Code Playgroud)

myInt的范围是0到99999.我想要的是将myStr作为5位数字.

例如:00072或09856或00002

谢谢.

objective-c nsstring nsinteger stringwithformat

1
推荐指数
1
解决办法
824
查看次数

"格式字符串不是字符串文字(可能不安全)"

我正在使用Xcode 4.6.我的应用程序正在使用设备和模拟器,但在构建时我收到警告:

"格式字符串不是字符串文字(可能不安全)"

从这段代码

[sArray addObject:[NSString stringWithFormat:subCatName]];

l.text= [spacing stringByAppendingFormat:[mArray objectAtIndex:section]];
Run Code Online (Sandbox Code Playgroud)

为什么,我该如何解决?

cocoa objective-c compiler-warnings nsstring stringwithformat

1
推荐指数
1
解决办法
2119
查看次数

在iPhone上正确重用NSMutableString

在iPhone上使用objective-c,这段代码出了什么问题?是在泄漏记忆吗?为什么?我该如何正确地做到这一点?

NSMutableString *result = [NSMutableString stringWithFormat:@"the value is %d", i];
Run Code Online (Sandbox Code Playgroud)

...然后在我的代码中...我可能需要将其更改为:

result = [NSMutableString stringWithFormat:@"the value is now %d", i];
Run Code Online (Sandbox Code Playgroud)

我需要第二次使用stringWithFormat ...但是不是创建一个新字符串并且没有正确释放旧字符串吗?

iphone memory-management objective-c stringwithformat nsmutablestring

0
推荐指数
1
解决办法
2083
查看次数

stringWithFormat:@"%@ - 1"是什么意思?

我正在阅读别人的代码,他们正在使用%@ - 1格式化整数.我无法在Google上找到任何内容,因为它忽略了符号.还有其他人比我更有经验吗?

[NSString stringWithFormat:@"%@-1", subnumber]

谢谢!

xcode objective-c stringwithformat

0
推荐指数
2
解决办法
1855
查看次数

为什么字符串长度为6?

NSString *str = nil;
NSLog(@"str = %@",str);
NSLog(@"str.length = %u",str.length);

NSString *str2 = [NSString stringWithFormat:@"%@",str];
NSLog(@"str2 = %@",str2);
NSLog(@"str2.length = %u",str2.length);
Run Code Online (Sandbox Code Playgroud)

NSLog模式是:

str = (null)
str.length = 0
str2 = (null)
str2.length = 6
Run Code Online (Sandbox Code Playgroud)

str2.length = 6让我困惑,为什么?

objective-c nsstring stringwithformat

-1
推荐指数
1
解决办法
54
查看次数

NSString 格式化

我正在更改一些预先编写的代码,视频时间标签的格式如下:

return [NSString stringWithFormat:@"%2d:%0.2d", minutes, seconds];
Run Code Online (Sandbox Code Playgroud)

发生的情况是,如果minutes是单个数字,它会在它之前返回一个空格,例如4:494 之前有一个空格。如果参数是 2 位数字minutes,但只有 1 个字符,如何格式化以使字符串中有 2 个字符如果minutes是1位数字?

objective-c nsstring stringwithformat

-1
推荐指数
1
解决办法
3364
查看次数