简体   繁体   English

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

[英]What does stringWithFormat:@“%@-1” mean?

I'm reading someone elses code and they are using %@-1 to format an integer. 我正在阅读其他人的代码,他们正在使用%@-1格式化整数。 I can't find anything on Google since it ignores symbols. 我在Google上找不到任何内容,因为它会忽略符号。 Anyone else had more experience at string formatting than me? 其他人比我有更多的字符串格式化经验吗?

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

Thanks! 谢谢!

That's just going to print "NUM-1" (where NUM is the number). 那只是打印“ NUM-1”(其中NUM是数字)。 To give an example, if the number is 5, that will print "5-1". 举个例子,如果数字为5,将显示“ 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). 在这种情况下,这意味着%@标记的任何修饰符都必须出现在%@ (尽管我不确定%@接受任何修饰符)。

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: 每个转换规范都由'%'字符或字符序列“%n $”引入,然后依次出现以下内容:

  • 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; 可选的精度,它为d,i,o,u,x和X转换说明符提供最少的位数。 the number of digits to appear after the radix character for the a, A, e, E, f, and F conversion specifiers; a,A,e,E,f和F转换说明符的基数字符后出现的位数; the maximum number of significant digits for the g and G conversion specifiers; g和G转换说明符的最大有效位数; or the maximum number of bytes to be printed from a string in the s [XSI] [Option Start] and S [Option End] conversion specifiers. 或s [XSI] [Option Start]和S [Option End]转换说明符中的字符串要打印的最大字节数。 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. @是一个转换指定符字符(如提到这里 ),这表明我们要访问传递中作为价值NSObject并读取其description属性。 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. 由于我们已经到达最后一个要点,因此格式代码实际上在@符号之后结束,并且正如@Kevin Ballard指出的那样, -1被解析为文字文本。

subnumber is probably object of class like NSNumber. 子编号可能是NSNumber之类的对象。 Like we use %d for int, %f for float, %@ is place holder for a refrence. 就像我们将%d用作int,将%f用作float一样,%@是用于占位符的引用。 In that case 在这种情况下

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

will print '5-1' 将打印“ 5-1”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM