Swift中将多个值设置为属性的等效方法

5

我目前正在使用一个用Objective-C编写的CocoaPod。在示例中,他们展示了以下内容:

options.allowedSwipeDirections = MDCSwipeDirectionLeft | MDCSwipeDirectionRight;

我甚至不知道这种变量叫什么,但我已经在Swift中尝试过以下内容:

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;

尝试将其像 [.Left, .Right] 这样使用 - Bhavin Bhadani
我已经删除了之前的评论。MDCSwipeDirection似乎来自https://github.com/modocache/MDCSwipeToChoose,其中它作为一个NS_ENUM在ObjC中定义,而不是NS_OPTIONS。 - Martin R
@MartinR 实际上这是一个分支:https://github.com/clsource/MDCSwipeToChoose 但唯一的区别应该是可以向上和向下滑动。 - Ybrin
4个回答

5

MDCSwipeDirection不幸地是以 Objective-C 的 NS_ENUM 形式而不是 NS_OPTIONS 形式定义的:

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

因此,它作为一个简单的enum而不是OptionSetType导入到Swift中:
public enum MDCSwipeDirection : Int {

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

因此,您需要处理 enum <-> Int 转换的 rawValue ,以便有效运用。
let allowedSwipeDirections =  MDCSwipeDirection(rawValue: MDCSwipeDirection.Left.rawValue | MDCSwipeDirection.Right.rawValue)!

请注意,强制解包不会失败,例如请参阅如何确定具有Swift 1.2的NS_ENUM的未记录值

如果枚举从NS_ENUM定义导入,则… Swift 1.2现在不允许使用任意原始值(基础整数类型)创建枚举变量。


如果您更改Objective-C定义为:

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

然后将其导入

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

而且您可以简单地编写

let allowedDirections : MDCSwipeDirection = [ .Left, .Right ]

1

我假设这个MDCSwipeDirection是符合OptionsSetType的结构体,例如以下内容:

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

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

在这种情况下,您可以使用类似数组的列表[..., ...]访问多个“case”(静态属性;选项):

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

适用于您的情况,具体而言。
options.allowedSwipeDirections = [.Left, .Right]

由于我们可以假设allowedSwipeDirections属性是MDCSwipeDirection类型,因此在以数组方式分组选项时,您可以省略此类型。

我不是Downvoter,但这给了我“上下文类型'MDCSwipeDirection'不能与数组文字一起使用”的错误提示。 - Ybrin
正如MartinR所指出的那样,这种情况下,NS_ENUM将被导入为一个Int类型的枚举,其包含.Left(rawvalue 0)和.Right(rawvalue 1)两个case。如果是这种情况,我不知道如何允许多个允许的滑动方向(就像使用符合OptionsSetType的类型一样),因为options.allowedSwipeDirections只接受.Left.Right,但不能同时存在。 - dfrib

1
您可以在Swift中像数组一样设置它们:
options.allowedSwipeDirections = [MDCSwipeDirection.Left, MDCSwipeDirection.Right]

“或者简写方式:”
options.allowedSwipeDirections = [.Left, .Right]

0

你需要一个选项集

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

请查看选项集这里


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接