:マッピング配列自身が表現可能なので、エラーが発生するのはなぜですか?私はこれを試してい
var orientations1: UIInterfaceOrientationMask = [.portrait, .landscapeRight]
var orientations2: UIInterfaceOrientationMask = [.portrait, .landscapeRight].map { $0 }
orientations1
が正常に動作しますが、orientation2
次のエラーを生成します。
Type of expression is ambiguous without more context
UIInterfaceOrientationMask
はExpressibleByArrayLiteral
プロトコルに間接的に準拠しています。 .map { $0 }
はおそらくアレイの内容を変更することができますし、それを動作させるにはどうすればよいですか?
オーケー
UPDATEので、ここで私が何をしようとしているのです。私はSwiftを学んでおり、実行時にデバイスの向きを変えて遊びたい。だから今、すべてのUI要素は、そのタグが適切な方向マスクの生の値に設定されたスイッチ
switchPortrait.tag = Int(UIInterfaceOrientationMask.portrait.rawValue)
switchLandscapeRight.tag = Int(UIInterfaceOrientationMask.landscapeRight.rawValue)
switchPortraitUpsideDown.tag = Int(UIInterfaceOrientationMask.portraitUpsideDown.rawValue)
switchLandscapeLeft.tag = Int(UIInterfaceOrientationMask.landscapeLeft.rawValue)
orientationSwitches = [switchPortrait, switchLandscapeRight, switchPortraitUpsideDown, switchLandscapeLeft]
for switchElement in orientationSwitches {
switchElement.isOn = orientations.contains(UIInterfaceOrientationMask(rawValue: UInt(switchElement.tag)))
}
:
@IBOutlet weak var switchPortrait: UISwitch!
@IBOutlet weak var switchLandscapeRight: UISwitch!
@IBOutlet weak var switchPortraitUpsideDown: UISwitch!
@IBOutlet weak var switchLandscapeLeft: UISwitch!
var orientationSwitches = [UISwitch]()
var orientations: UIInterfaceOrientationMask = [.portrait, .landscapeRight, .landscapeLeft, .portrait]
方法viewDidLoad()
では、私はこれやってる:
私は4つのスイッチを持っています。また、初期状態であるorientations
に従って、すべてのスイッチがオンまたはオフになります。スイッチング動作で
、私はこのようなorientations
更新しようとしている:
@IBAction func orientationSwitched(_ sender: UISwitch) {
orientations = orientationSwitches.filter({ $0.isOn }).map({ UIInterfaceOrientationMask(rawValue: UInt($0.tag)) }) // doesn't work
}
をしかし、それは動作しません。そして、私はsupportedInterfaceOrientations
は、単に次のようになります。
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return orientations
}
私は、各UIスイッチ素子用insert()
メソッドを介してそれを行うことができるんだけど、私はそれがマッピングによって、よりエレガントな方法で行うことができるかどうか知りたいです。
'UIInterfaceOrientationMask'は、* *のは、option *なく*の配列です。したがって、配列リテラルで初期化することはできますが、配列に代入することはできません。これは 'map'が返すものです。 – vacawama
恐ろしいですが、動作します: 'var orientations2:UIInterfaceOrientationMask = [UIInterfaceOrientationMask.portrait、.landscapeRight] .map {$ 0} .reduce(UIInterfaceOrientationMask()){var result = $ 0; result.insert($ 1); return result} ' – vacawama
@vacawama、あなたは' union'を使うことができ、 'UIInterfaceOrientationMask()'は '[]'で置き換えることができます。 – OOPer