5

I am currently using a cocoapod which was written in objective c. In the example they show something like that:

options.allowedSwipeDirections = MDCSwipeDirectionLeft | MDCSwipeDirectionRight;

I don't even know what these sort of variables are called but I already tried the following in Swift:

options.allowedSwipeDirections = MDCSwipeDirection.Left | MDCSwipeDirection.Right

But the compiler says No '|' candidates produce the expected contextual result type 'MDCSwipeDirection'

How would I do this in Swift?

Edit:

It looks like this is not an OptionSet as stated in some answers, her is the declaration:

/*!
 * 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;

and it is used like that:

_allowedSwipeDirections = MDCSwipeDirectionLeft | MDCSwipeDirectionRight;
Ybrin
  • 901
  • 8
  • 30
  • try to use it like `[.Left, . Right]` – Bhavin Bhadani Apr 07 '16 at 09:25
  • I have removed my previous comment. MDCSwipeDirection seems to come from https://github.com/modocache/MDCSwipeToChoose where is its defined in ObjC as an NS_ENUM, not NS_OPTIONS. – Martin R Apr 07 '16 at 09:38
  • @MartinR Actually it is a fork: https://github.com/clsource/MDCSwipeToChoose but the only differency should be the possibility to swipe up and down – Ybrin Apr 07 '16 at 09:41

4 Answers4

5

MDCSwipeDirection is (unfortunately) defined in Objective-C as an NS_ENUM and not as NS_OPTIONS:

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

It is therefore imported to Swift as a simple enum and not as an OptionSetType:

public enum MDCSwipeDirection : Int {

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

Therefore you have to juggle with rawValue for enum <-> Int conversions:

let allowedSwipeDirections =  MDCSwipeDirection(rawValue: MDCSwipeDirection.Left.rawValue | MDCSwipeDirection.Right.rawValue)!

Note that the forced unwrap cannot fail, see for example How to determine if undocumented value for NS_ENUM with Swift 1.2:

... Swift 1.2 does now allow the creation of enumeration variables with arbitrary raw values (of the underlying integer type), if the enumeration is imported from an NS_ENUM definition.


If you change the Objective-C definition to

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

then it is imported as

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

and you can simply write

let allowedDirections : MDCSwipeDirection = [ .Left, .Right ]
Community
  • 1
  • 1
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
1

I will assume that this MDCSwipeDirection is a structure conforming to OptionsSetType, e.g. something along

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

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

in which case you can access several "cases" (static properties; options) simply using array-like listing [..., ...]:

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

Applied to your case, specifically

options.allowedSwipeDirections = [.Left, .Right]

As we can assume that allowedSwipeDirections property is of type MDCSwipeDirection, hence you can omit this type when grouping your options array-style.

EmilioPelaez
  • 18,758
  • 6
  • 46
  • 50
dfrib
  • 70,367
  • 12
  • 127
  • 192
  • I am not the Downvoter but this gives me `Contextual type 'MDCSwipeDirection' cannot be used with array literal` – Ybrin Apr 07 '16 at 09:35
  • @Ybrin As [MartinR](https://github.com/modocache/MDCSwipeToChoose/blob/master/MDCSwipeToChoose/Public/State/MDCSwipeDirection.h) pointed out. In this case, the `NS_ENUM` will just be imported to Swift as an `Int`-type enumeration with cases `.Left` (rawvalue 0) and `.Right` (rawvalue 1). If this is the case, I don't know how to allow several allowed swipe cases (as neatly done using `OptionsSetType` conforming types), as `options.allowedSwipeDirections` simply expect only `.Left` or `.Right`, but not both. – dfrib Apr 07 '16 at 09:48
1

You can set them like an array in Swift:

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

Or shorthand:

options.allowedSwipeDirections = [.Left, .Right]
Joe Benton
  • 3,703
  • 1
  • 21
  • 17
0

You need an option set

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

Have a look at Option Sets here

ielyamani
  • 17,807
  • 10
  • 55
  • 90