LoginSignup
16
19

More than 1 year has passed since last update.

Objective-C で OS バージョン判定を簡潔に行う方法

Last updated at Posted at 2017-10-30

概要

OS のバージョンを調べる方法が、Objective-C だと面倒くさい方法しかないのかと思ってて、うんざりしてたら、そんなことなかったので紹介。

環境

  • MacOS 10.12.6
  • Xcode 9.0.1

従来の方法

記述量が多くて面倒だった。

if ([UIDevice currentDevice].systemVersion.floatValue >= 11.0) {
    NSLog(@"iOS11以上だよ");
} else {
    NSLog(@"iOS11未満だよ");
}

簡潔な方法

記述量が少なくて良い。

if (@available(iOS 11.0, *)) {
    NSLog(@"iOS11以上だよ");
} else {
    NSLog(@"iOS11未満だよ");
}

注意点

@available(〜) と他の条件を一緒に記載すると warning が出る。
ネストすれば大丈夫。

NG

Bool isClose = YES;
if (@available(iOS 11.0, *) && isClose) {
    NSLog(@"iOS11以上だよ");
}

OK

Bool isClose = YES;
if (@available(iOS 11.0, *)) {
    if (isClose) {
        NSLog(@"iOS11以上だよ");
    }
}

ネストせずに書けたらいいなぁ。

参考:Negating Objective-C's @available keyword

まとめ

新しいやり方がないか、定期的に確認しないとダメだね。

16
19
1

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
16
19