LoginSignup
1
1

More than 3 years have passed since last update.

【Objective-C】Objective-Cでswiftの??的なことをやるには

Posted at

Objective-CでSwiftの??的な事を実現するには

Swiftでは??というnil結合演算子が用意されています。
??を使用すれば変数値がnilだった場合に値を設定することができます。

Swift

var color = nil
var selectColor = color ?? "blue"

以上のような事をObjective-Cでもできるみたいなので紹介します。

Objective-C

NSString *test = [[NSString alloc] init];
NSString *test2 = test ?: @"hogehoge";

Objective-Cでは以上のように?:を用いる事で同じことができるようです。
Swiftと違う点は??がnil結合演算子になのに対し、?:は三項演算子を省略したものであるということです。
詳しくは参考URLを下記に記載しているので、そこからご確認ください。

参考

1
1
3

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
1
1