LoginSignup
9
8

More than 5 years have passed since last update.

Swift 2でビット演算子|を使うとエラーになる場合

Posted at

以下のコードはSwift 2ではエラーとなります。

example1.swift
let view = UIView()
view.autoresizingMask = .FlexibleTopMargin | .FlexibleBottomMargin

Type of expression is ambiguous without more context

これはSwift 1.2ではRawOptionSetTypeだったものがSwift 2.0でOptionSetTypeに変わったためです。
RawOptionSetTypeではビット演算が可能です。

Swift Standard Library Reference OptionSetType Protocol Reference

Swift 2では以下のように記述します。

example2.swift
let view = UIView()
view.autoresizingMask = [.FlexibleTopMargin, .FlexibleBottomMargin]
9
8
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
8