Domanda

Hi I get this warning when using %lu:

"format specifies type unsigned long but the argument has type unsigned int."

then when i replace to %u: the warning changes to:

"format specifies type unsigned int but the argument has type unsigned long."

here is my code:

NSString *viewName_ = [NSString stringWithFormat:@"myGreatView%lu", currentTagLoadedOnView-99];

where currentTagLoadedOnView:

NSUInteger currentTagLoadedOnView;

any help greatly appreciated....

È stato utile?

Soluzione 2

Try any one of the below

1) Use both as unsigned int

NSString *viewName_ = [NSString stringWithFormat:@"myGreatView%u", currentTagLoadedOnView-99];

and declare currentTagLoadedOnView as NSUInteger currentTagLoadedOnView;

2) type cast to unsigned long

NSString *viewName_ = [NSString stringWithFormat:@"myGreatView%lu", (unsigned long) currentTagLoadedOnView-99];

and declare currentTagLoadedOnView as NSUInteger currentTagLoadedOnView;

3) Defined unsinged long as both

NSString *viewName_ = [NSString stringWithFormat:@"myGreatView%lu", (unsigned long) currentTagLoadedOnView-99];

and declare currentTagLoadedOnView as unsigned long currentTagLoadedOnView;

Note: Your warning will disappear if you use 64-bit architecture only(make changes to consistent data type) or 32-bit only(no change). If you use Standard architecture(including 64-bit), it will give this warning.

Altri suggerimenti

The actual type of NSUInteger is different depending on whether you compile for 32 bit or 64 bit. Therefore, whatever format you use, you will get a warning, either in the 32 bit build or in the 64 bit build. Solution is quite easy:

NSString *viewName_ = [NSString stringWithFormat:@"myGreatView%lu", (unsigned long) (currentTagLoadedOnView-99)];

NSUInteger can be cast to unsigned long without loss of information, both on 32 bit and 64 bit systems.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top