XCode,在此检查有效的png代码中修复始终为true的警告

时间:2014-01-07 05:57:35

标签: ios xcode compiler-warnings

XCode5中,我有这段代码

1  - (BOOL)checkValidPNGImage{

2        NSData *imagedata = [NSData dataWithContentsOfFile:self.imageFullPath];
3        if ([imagedata length] < 4)
4            return NO;    
5         const char * bytes = (const char *)[imagedata bytes];    
6         if (bytes[0] != 0x89 || bytes[1] != 0x50)
7             return NO;
8         if (bytes[[imagedata length] - 2] != 0x60 ||
9             bytes[[imagedata length] - 1] != 0x82)
10            return NO;
11    return YES;
12 }

在第6行和第8行发出警告

Comparison of constant 137 with expression of type 'const char' is always true

Comparison of constant 130 with expression of type 'const char' is always true

如何解决这个问题?顺便说一下,我从上面的某个地方得到了上面的代码,所以我打开任何其他选项来检查有效的png

2 个答案:

答案 0 :(得分:5)

char从-128到127.所以它不能超过0x7f。

-

使用unsigned char代替

答案 1 :(得分:3)

使用此代替const char * bytes = (const char *)[imagedata bytes];

const unsigned char * bytes = (const unsigned char *)[imagedata bytes];