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

发布于 2024-12-13 13:08:59 字数 161 浏览 1 评论 0 原文

我正在阅读其他人的代码,他们正在使用 %@-1 来格式化整数。我在谷歌上找不到任何东西,因为它忽略了符号。还有人比我在字符串格式化方面更有经验吗?

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

谢谢!

I'm reading someone elses code and they are using %@-1 to format an integer. I can't find anything on Google since it ignores symbols. Anyone else had more experience at string formatting than me?

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

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

‘画卷フ 2024-12-20 13:08:59

根据规范

每个转换规范均由“%”字符或字符序列“%n$”引入,其后按顺序出现以下内容:

  • 零个或多个标志(以任何顺序),它们修改转换规范的含义。

  • 可选的最小字段宽度。如果转换后的值的字节数小于字段宽度,则默认在左侧填充空格;如果为字段宽度指定了如下所述的左调整标志('-'),则应在右侧填充它。字段宽度采用星号 ( '*' ) 的形式(如下所述)或十进制整数。

  • 可选精度,给出 d、i、o、u、x 和 X 转换说明符出现的最小位数; a、A、e、E、f 和 F 转换说明符的基数字符后出现的位数; g 和 G 转换说明符的最大有效位数;或从 s [XSI] [Option Start] 和 S [Option End] 转换说明符中的字符串打印的最大字节数。精度采用句点 ( '.' ) 后跟星号 ( '*' ) 的形式(如下所述),或可选的十进制数字字符串,其中空数字字符串被视为零。如果精度与任何其他转换说明符一起出现,则行为未定义。

  • 一个可选的长度修饰符,用于指定参数的大小。

  • 转换说明符,指示要应用的转换类型。

我们使用第一种类型的转换,因为这里没有美元符号。请注意上面列表顶部的单词按顺序@ 是一个转换说明符(如前所述此处),这表明我们应该访问作为 NSObject 传入的值并读取其 description 属性。由于我们已经到达最后一个要点,因此格式代码实际上在 @ 符号之后结束,并且正如 @Kevin Ballard 指出的那样,-1 被解析为文字文本。

According to the specification:

Each conversion specification is introduced by the '%' character, or by the character sequence "%n$", after which the following appear in sequence:

  • Zero or more flags (in any order), which modify the meaning of the conversion specification.

  • An optional minimum field width. If the converted value has fewer bytes than the field width, it shall be padded with spaces by default on the left; it shall be padded on the right if the left-adjustment flag ( '-' ), described below, is given to the field width. The field width takes the form of an asterisk ( '*' ), described below, or a decimal integer.

  • An optional precision that gives the minimum number of digits to appear for the d, i, o, u, x, and X conversion specifiers; the number of digits to appear after the radix character for the a, A, e, E, f, and F conversion specifiers; the maximum number of significant digits for the g and G conversion specifiers; or the maximum number of bytes to be printed from a string in the s [XSI] [Option Start] and S [Option End] conversion specifiers. The precision takes the form of a period ( '.' ) followed either by an asterisk ( '*' ), described below, or an optional decimal digit string, where a null digit string is treated as zero. If a precision appears with any other conversion specifier, the behavior is undefined.

  • An optional length modifier that specifies the size of the argument.

  • A conversion specifier character that indicates the type of conversion to be applied.

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 an NSObject and read its description 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.

淡笑忘祈一世凡恋 2024-12-20 13:08:59

这只是打印“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).

苏辞 2024-12-20 13:08:59

subnumber 可能是像 NSNumber 这样的类的对象。就像我们使用 %d 表示 int、%f 表示 float 一样,%@ 是引用的占位符。在这种情况下

NSNumber *subnumber = [NSNumber numberWithInt:5];
NSLog([NSString stringWithFormat:@"%@-1", subnumber]);

将打印“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

NSNumber *subnumber = [NSNumber numberWithInt:5];
NSLog([NSString stringWithFormat:@"%@-1", subnumber]);

will print '5-1'

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文