2016-04-07 8 views
2

私は現在、目的cで書かれたココアポードを使用しています。 の例では、彼らがそのようなことを示しています。プロパティに複数の値を設定するスウィフト同等物

options.allowedSwipeDirections = MDCSwipeDirectionLeft | MDCSwipeDirectionRight; 

を私も変数のこれらの並べ替えが呼び出されるかわからないが、私はすでにスウィフトに次のように試してみました:

options.allowedSwipeDirections = MDCSwipeDirection.Left | MDCSwipeDirection.Right 

しかし、コンパイラは述べていますNo '|' candidates produce the expected contextual result type 'MDCSwipeDirection'

私はこれをSwiftでどうやって行いますか?

編集:いくつかの回答で述べたように、これはOptionSetではないように見えます

、彼女は宣言です:

/*! 
* Contains the directions on which the swipe will be recognized 
* Must be set using a OR operator (like MDCSwipeDirectionUp | MDCSwipeDirectionDown) 
*/ 
@property (nonatomic, assign) MDCSwipeDirection allowedSwipeDirections; 

、それはそのように使用されます。

_allowedSwipeDirections = MDCSwipeDirectionLeft | MDCSwipeDirectionRight; 
+0

'[。左の。右] ' –

+0

私は前のコメントを削除しました。 MDCSwipeDirectionはhttps://github.com/modocache/MDCSwipeToChooseから来ているようですが、どこにNS_OPTIONSではなくNS_ENUMとしてObjCで定義されていますか? –

+0

@MartinR実際にはフォークです:https://github.com/clsource/MDCSwipeToを選択してください。唯一の差異は上下にスワイプする可能性があります。 – Ybrin

答えて

5

MDCSwipeDirectionは(残念ながら)NS_OPTIONSとしてNS_ENUMとしないようにObjective-Cの で定義されている:

public enum MDCSwipeDirection : Int { 

    case None = 1 
    case Left = 2 
    case Right = 4 
    case Up = 8 
    case Down = 16 
} 

typedef NS_ENUM(NSInteger, MDCSwipeDirection) { 
    MDCSwipeDirectionNone = 1, 
    MDCSwipeDirectionLeft = 2, 
    MDCSwipeDirectionRight = 4, 
    MDCSwipeDirectionUp = 8, 
    MDCSwipeDirectionDown = 16 
}; 

従ってOptionSetTypeとして単純enum なくとしてスウィフトにインポートされ

enum <-> Int のコンバージョン:

の場合、 rawValueと混乱する必要があります。強制アンラップは、失敗例 How to determine if undocumented value for NS_ENUM with Swift 1.2ために見ることができない
let allowedSwipeDirections = MDCSwipeDirection(rawValue: MDCSwipeDirection.Left.rawValue | MDCSwipeDirection.Right.rawValue)! 

注:

...スウィフト1.2になりました(基本となる整数型の) 任意の生の値を持つ列挙変数を作成することができません、 列挙をNS_ENUM定義からインポートする場合。


場合、あなたはそれが

public struct MDCSwipeDirection : OptionSetType { 
    public init(rawValue: Int) 

    public static var None: MDCSwipeDirection { get } 
    public static var Left: MDCSwipeDirection { get } 
    public static var Right: MDCSwipeDirection { get } 
    public static var Up: MDCSwipeDirection { get } 
    public static var Down: MDCSwipeDirection { get } 
} 

としてインポートされ、その後

typedef NS_OPTIONS(NSInteger, MDCSwipeDirection) { 
    MDCSwipeDirectionNone = 1, 
    MDCSwipeDirectionLeft = 2, 
    MDCSwipeDirectionRight = 4, 
    MDCSwipeDirectionUp = 8, 
    MDCSwipeDirectionDown = 16 
}; 

にObjective-Cの定義を変更すると、あなたは単に

0を書き込むことができます
let allowedDirections : MDCSwipeDirection = [ .Left, .Right ] 
1

このMDCSwipeDirectionは、OptionsSetTypeに準拠した構造であると仮定します。 、あなたのケースに特異的に

options.allowedSwipeDirections = [.Left, .Right] 

var myChoices : SomeOptionSetType = [.Left, .Right] 

応用:単純に配列のようなリスト[..., ...]を使用して、あなたは、いくつかの「例」(オプション静的プロパティ)にアクセスすることができ、その場合には

struct SomeOptionSetType : OptionSetType { 
    let rawValue : Int 
    init(rawValue:Int){ self.rawValue = rawValue} 

    static let Left = SomeOptionSetType(rawValue:1) 
    static let Right = SomeOptionSetType(rawValue:2) 
} 

に沿って何か

allowedSwipeDirectionsプロパティのタイプはMDCSwipeDirectionなので、オプションをグループ化するときにこのタイプを省略することができます。array-styル。

+0

私はDownvoterではありませんが、これは私に 'Contextual type 'MDCSwipeDirection'配列リテラルで使用される – Ybrin

+0

更新された回答 – Ybrin

+0

を参照してください。[MartinR](https://github.com/modocache/MDCSwipeToChoose/blob/master/MDCSwipeToChoose/Public/State/MDCSwipeDirection.h)が指摘しています。この場合、 'NS_ENUM'は' .Left'(rawvalue 0)と '.Right'(rawvalue 1)というケースを持つ' Int'型列挙としてSwiftにインポートされます。この場合、 'options.allowedSwipeDirections'は単に' .Left'や '.Left'だけを期待しているので、いくつかの許可されたスワイプケースを許可する方法は分かりません(' OptionsSetType'準拠の型を使ってきれいに)。両方ともではない。 – dfri

1

あなたはスウィフトの配列のようにそれらを設定することができます。

options.allowedSwipeDirections = [MDCSwipeDirection.Left, MDCSwipeDirection.Right] 

または速記:あなたはオプションが

options.allowedSwipeDirections = [MDCSwipeDirection.Left, MDCSwipeDirection.Right] 

を設定する必要

options.allowedSwipeDirections = [.Left, .Right] 
0

は、オプション・セットを見てくださいhere

関連する問題