9
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[iPhone]32bit端末と64bit端末でBOOLへのキャスト結果が異なる!

Last updated at Posted at 2015-04-23

開発中にハマったので共有。

32bit端末では1バイトより大きい数値をBOOLにキャストするとNOになってしまう。
OSは無関係のようだ。

32bit端末 64bit端末
(BOOL)255 YES YES
(BOOL)256 NO YES

sizeof(BOOL)はどちらの端末でも1バイトなので、どうしてこうなってしまうかは不明。

なので下のようにNS_OPTIONS値を使ってフラグ判定する場合は注意が必要。

typedef NS_OPTIONS(NSUInteger, SomeFlags){
    kFLG1 = 1 << 0,
    kFLG2 = 1 << 1,
    ....
    kFLG8 = 1 << 7,
    kFLG9 = 1 << 8
};

// OK
- (BOOL)isFLG9:(NSUInteger)value {
    return ((value & kFLG9) != 0);
}

// NG
- (BOOL)isFLG9:(NSUInteger)value {
    return (value & kFLG9); // 結果が0以外でも32bit端末だとNOが返ってしまう
}
9
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?