문제

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....

도움이 되었습니까?

해결책 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.

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top