如何将NSUInteger转换为NSString?

时间:2022-09-15 21:41:46

How do you convert an NSUInteger into an NSString? I've tried but my NSString returned 0 all the time.

如何将NSUInteger转换为NSString?我试过但是我的NSString一直都是0。

NSUInteger NamesCategoriesNSArrayCount = [self.NamesCategoriesNSArray count];  
NSLog(@"--- %d", NamesCategoriesNSArrayCount);  
[NamesCategoriesNSArrayCountString setText:[NSString stringWithFormat:@"%d",    NamesCategoriesNSArrayCount]];  
NSLog(@"=== %d", NamesCategoriesNSArrayCountString);

5 个解决方案

#1


32  

When compiling with support for arm64, this won't generate a warning:

当编译支持arm64时,这不会产生警告:

[NSString stringWithFormat:@"%lu", (unsigned long)myNSUInteger];

#2


7  

I hope your NamesCategoriesNSArrayCountString is NSString; if yes use the below line of code.

我希望你的NamesCategoriesNSArrayCountString是NSString;如果是,请使用以下代码行。

NamesCategoriesNSArrayCountString  = [NSString stringWithFormat:@"%d", NamesCategoriesNSArrayCount]];

istead of

不是

[NamesCategoriesNSArrayCountString setText:[NSString stringWithFormat:@"%d", NamesCategoriesNSArrayCount]];

#3


5  

You can also use:

您还可以使用:

NSString *rowString = [NSString stringWithFormat: @"%@",  @(row)];

where row is a NSUInteger.

其中row是NSUInteger。

#4


4  

When compiling for arm64, use the following to avoid warnings:

编译arm64时,请使用以下命令以避免警告:

[NSString stringWithFormat:@"%tu", myNSUInteger];

Or, in your case:

或者,在您的情况下:

NSUInteger namesCategoriesNSArrayCount = [self.NamesCategoriesNSArray count];  
NSLog(@"--- %tu", namesCategoriesNSArrayCount);  
[namesCategoriesNSArrayCountString setText:[NSString stringWithFormat:@"%tu", namesCategoriesNSArrayCount]];  
NSLog(@"=== %@", namesCategoriesNSArrayCountString);

(Also, tip: Variables start with lowercase. Info: here)

(另外,提示:变量以小写开头。信息:这里)

#5


0  

This String Format Specifiers article from Apple is specific when you need to format Apple types:

当您需要格式化Apple类型时,Apple的此字符串格式说明文章是特定的:

OS X uses several data types—NSInteger, NSUInteger,CGFloat, and CFIndex—to provide a consistent means of representing values in 32- and 64-bit environments. In a 32-bit environment, NSInteger and NSUInteger are defined as int and unsigned int, respectively. In 64-bit environments, NSInteger and NSUInteger are defined as long and unsigned long, respectively. To avoid the need to use different printf-style type specifiers depending on the platform, you can use the specifiers shown in Table 3. Note that in some cases you may have to cast the value.

OS X使用多种数据类型-NSInteger,NSUInteger,CGFloat和CFIndex-来提供在32位和64位环境中表示值的一致方法。在32位环境中,NSInteger和NSUInteger分别定义为int和unsigned int。在64位环境中,NSInteger和NSUInteger分别定义为long和unsigned long。为了避免根据平台使用不同的printf样式类型说明符,可以使用表3中显示的说明符。请注意,在某些情况下,您可能必须转换值。

  • [NSString stringWithFormat:@"%ld", (long)value] : NSInteger displayed as decimal
  • [NSString stringWithFormat:@“%ld”,(long)value]:NSInteger显示为十进制
  • [NSString stringWithFormat:@"%lx", (long)value] : NSInteger displayed as hex
  • [NSString stringWithFormat:@“%lx”,(long)value]:NSInteger显示为十六进制
  • [NSString stringWithFormat:@"%lu", (unsigned long)value] : NSUInteger displayed as decimal
  • [NSString stringWithFormat:@“%lu”,(unsigned long)value]:NSUInteger显示为十进制
  • [NSString stringWithFormat:@"%lx", (unsigned long)value] : NSUInteger displayed as hex
  • [NSString stringWithFormat:@“%lx”,(unsigned long)value]:NSUInteger显示为十六进制
  • [NSString stringWithFormat:@"%f", value] : CGFloat
  • [NSString stringWithFormat:@“%f”,value]:CGFloat
  • [NSString stringWithFormat:@"%ld", (long)value] : CFIndex displayed as decimal
  • [NSString stringWithFormat:@“%ld”,(long)value]:CFIndex显示为十进制
  • [NSString stringWithFormat:@"%lx", (long)value] : CFIndex displayed as hex
  • [NSString stringWithFormat:@“%lx”,(long)value]:CFIndex显示为十六进制

See the article for more details.

有关详细信息,请参阅文章。

#1


32  

When compiling with support for arm64, this won't generate a warning:

当编译支持arm64时,这不会产生警告:

[NSString stringWithFormat:@"%lu", (unsigned long)myNSUInteger];

#2


7  

I hope your NamesCategoriesNSArrayCountString is NSString; if yes use the below line of code.

我希望你的NamesCategoriesNSArrayCountString是NSString;如果是,请使用以下代码行。

NamesCategoriesNSArrayCountString  = [NSString stringWithFormat:@"%d", NamesCategoriesNSArrayCount]];

istead of

不是

[NamesCategoriesNSArrayCountString setText:[NSString stringWithFormat:@"%d", NamesCategoriesNSArrayCount]];

#3


5  

You can also use:

您还可以使用:

NSString *rowString = [NSString stringWithFormat: @"%@",  @(row)];

where row is a NSUInteger.

其中row是NSUInteger。

#4


4  

When compiling for arm64, use the following to avoid warnings:

编译arm64时,请使用以下命令以避免警告:

[NSString stringWithFormat:@"%tu", myNSUInteger];

Or, in your case:

或者,在您的情况下:

NSUInteger namesCategoriesNSArrayCount = [self.NamesCategoriesNSArray count];  
NSLog(@"--- %tu", namesCategoriesNSArrayCount);  
[namesCategoriesNSArrayCountString setText:[NSString stringWithFormat:@"%tu", namesCategoriesNSArrayCount]];  
NSLog(@"=== %@", namesCategoriesNSArrayCountString);

(Also, tip: Variables start with lowercase. Info: here)

(另外,提示:变量以小写开头。信息:这里)

#5


0  

This String Format Specifiers article from Apple is specific when you need to format Apple types:

当您需要格式化Apple类型时,Apple的此字符串格式说明文章是特定的:

OS X uses several data types—NSInteger, NSUInteger,CGFloat, and CFIndex—to provide a consistent means of representing values in 32- and 64-bit environments. In a 32-bit environment, NSInteger and NSUInteger are defined as int and unsigned int, respectively. In 64-bit environments, NSInteger and NSUInteger are defined as long and unsigned long, respectively. To avoid the need to use different printf-style type specifiers depending on the platform, you can use the specifiers shown in Table 3. Note that in some cases you may have to cast the value.

OS X使用多种数据类型-NSInteger,NSUInteger,CGFloat和CFIndex-来提供在32位和64位环境中表示值的一致方法。在32位环境中,NSInteger和NSUInteger分别定义为int和unsigned int。在64位环境中,NSInteger和NSUInteger分别定义为long和unsigned long。为了避免根据平台使用不同的printf样式类型说明符,可以使用表3中显示的说明符。请注意,在某些情况下,您可能必须转换值。

  • [NSString stringWithFormat:@"%ld", (long)value] : NSInteger displayed as decimal
  • [NSString stringWithFormat:@“%ld”,(long)value]:NSInteger显示为十进制
  • [NSString stringWithFormat:@"%lx", (long)value] : NSInteger displayed as hex
  • [NSString stringWithFormat:@“%lx”,(long)value]:NSInteger显示为十六进制
  • [NSString stringWithFormat:@"%lu", (unsigned long)value] : NSUInteger displayed as decimal
  • [NSString stringWithFormat:@“%lu”,(unsigned long)value]:NSUInteger显示为十进制
  • [NSString stringWithFormat:@"%lx", (unsigned long)value] : NSUInteger displayed as hex
  • [NSString stringWithFormat:@“%lx”,(unsigned long)value]:NSUInteger显示为十六进制
  • [NSString stringWithFormat:@"%f", value] : CGFloat
  • [NSString stringWithFormat:@“%f”,value]:CGFloat
  • [NSString stringWithFormat:@"%ld", (long)value] : CFIndex displayed as decimal
  • [NSString stringWithFormat:@“%ld”,(long)value]:CFIndex显示为十进制
  • [NSString stringWithFormat:@"%lx", (long)value] : CFIndex displayed as hex
  • [NSString stringWithFormat:@“%lx”,(long)value]:CFIndex显示为十六进制

See the article for more details.

有关详细信息,请参阅文章。