Convert NSUInteger to string with ARC

10,032

You probably have a variable of type NSUInteger, something like

NSUInteger myNumber;

Then you can convert it to a string like this:

NSString *text = [NSString stringWithFormat:@"%li",  myNumber];

A solution that I prefer now is this:

NSString *text = [NSString stringWithFormat:@"%@",  @(myNumber)];   

This helps avoid compile warnings about incorrect number formatting codes (after a long time I still get confused in them).

Share:
10,032
tangobango
Author by

tangobango

Updated on June 22, 2022

Comments

  • tangobango
    tangobango about 2 years

    I'm trying to cast a NSUInteger to a string so I can print a message. From searching, it seems like I need to use stringWithFormat, but I am getting an error that an implicit cast not allowed with ARC.

    Here's the line in question:

    NSString *text = [[NSString stringWithFormat: (@"%li",  NSUInteger)];
    

    I've tried changing the format specifier to %lu with no help.

    Thanks.

    • limon
      limon about 10 years
      try [NSString stringWithFormat: @"%li",unsignedinteger];
    • rmaddy
      rmaddy about 10 years
      Why the parentheses? Where's the value you are trying to format?
    • rmaddy
      rmaddy about 10 years
      @matt - wrong duplicate. This question boils down to a problem with the invalid parentheses and not a problem with type conversions.
    • matt
      matt about 10 years
      @rmaddy the duplicate shows the exact correct form of the line of code to use
    • rmaddy
      rmaddy about 10 years
      @matt But it's a different question. Just because the answer is similar does not make it a duplicate question.
    • tangobango
      tangobango about 10 years
      @matt Does it matter that the duplicate doesn't show on search becasue the error messages are different? The other one doesn't reference ARC which seems to be the issue that the error addresses.
  • tangobango
    tangobango about 10 years
    I do have an NSUInteger in a variable but the line gives me the error that the "implicit cast of NSUInteger not allowed under ARC." Your solution is what I have found online but it doesn't work for me.
  • rmaddy
    rmaddy about 10 years
    Get rid of the parentheses.
  • TotoroTotoro
    TotoroTotoro about 10 years
    @rmaddy thanks. I copy-pasted the original code and didn't notice them.
  • TotoroTotoro
    TotoroTotoro about 10 years
    @tangobango try now. I've updated the code.
  • tangobango
    tangobango about 10 years
    @maddy That solved it. Thanks. Adding the parens was something I had found online and didn't remove them after that idea failed.
  • fatuhoku
    fatuhoku about 9 years
    Does anyone know whether wrapping the number up in a literal like @(x) has a performance hit or not?
  • TotoroTotoro
    TotoroTotoro about 9 years
    @fatuhoku it probably does, since I think it converts the primitive value to an NSNumber. But you have to ask yourself if this operation happens often enough to matter. Printing to console is expensive to begin with, and if you write to console a lot, it's a problem, with or without using the @() syntax.