stringWithFormat:@"%@-1" 是什么意思意思是?
我正在阅读其他人的代码,他们正在使用 %@-1 来格式化整数。我在谷歌上找不到任何东西,因为它忽略了符号。还有人比我在字符串格式化方面更有经验吗?
[NSString stringWithFormat:@"%@-1", subnumber]
谢谢!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据规范:
我们使用第一种类型的转换,因为这里没有美元符号。请注意上面列表顶部的单词按顺序。
@
是一个转换说明符(如前所述此处),这表明我们应该访问作为NSObject
传入的值并读取其description
属性。由于我们已经到达最后一个要点,因此格式代码实际上在@
符号之后结束,并且正如 @Kevin Ballard 指出的那样,-1
被解析为文字文本。According to the specification:
We're using a conversion of the first type, since there's no dollar sign in here. Note the words in sequence at the top of the above list. The
@
is a conversion specifier character (as mentioned here), which indicates that we should access the value passed in as anNSObject
and read itsdescription
property. Since we've already reached the last bullet point, the format code actually ends after the@
symbol, and as @Kevin Ballard pointed out, the-1
is parsed as literal text.这只是打印“NUM-1”(其中 NUM 是数字)。举个例子,如果数字是5,就会打印“5-1”。
使用格式字符串时,格式标记的任何修饰符都必须出现在格式类型说明符之前。在这种情况下,这意味着
%@
标记的任何修饰符都必须出现在%
和@
之间(尽管我不确定是否实际上有%@
接受的任何修饰符)。That's just going to print "NUM-1" (where NUM is the number). To give an example, if the number is 5, that will print "5-1".
When using format strings, any modifiers to the format token must occur before the format type specifier. In this case, that means any modifiers to the
%@
token must occur between the%
and the@
(though I'm not sure if there are actually any modifiers that%@
accepts).subnumber 可能是像 NSNumber 这样的类的对象。就像我们使用 %d 表示 int、%f 表示 float 一样,%@ 是引用的占位符。在这种情况下
将打印“5-1”
subnumber is probably object of class like NSNumber. Like we use %d for int, %f for float, %@ is place holder for a refrence. In that case
will print '5-1'