Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
19
Help us understand the problem. What is going on with this article?
@blackcat

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

More than 3 years have passed since last update.

概要

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

まとめ

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

19
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
blackcat
黒猫好きなプログラマ。

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
19
Help us understand the problem. What is going on with this article?