【问题标题】:XCode, fix always true warning in this check valid png codeXCode,在此检查有效的 png 代码中修复始终为真警告
【发布时间】:2014-01-07 05:57:35
【问题描述】:

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

【问题讨论】:

标签: ios xcode compiler-warnings


【解决方案1】:

char 是从-128 到 127,所以不能超过 0x7f。

-

改用无符号字符

【讨论】:

  • 所以,我必须把 char 改成什么?,把它改成 Byte 省钱吗?
  • @DeckyFx 使用无符号字符。
【解决方案2】:

用这个代替const char * bytes = (const char *)[imagedata bytes];

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

【讨论】:

    猜你喜欢
    • 2018-09-22
    • 2019-09-27
    • 1970-01-01
    • 2015-01-30
    • 2020-10-27
    • 2021-05-11
    • 2019-12-07
    • 1970-01-01
    • 2023-01-04
    相关资源
    最近更新 更多