Ios Swift等效于为属性设置多个值

Ios Swift等效于为属性设置多个值,ios,objective-c,swift,Ios,Objective C,Swift,我目前使用的cocoapod是用objective c编写的。 在示例中,它们显示了类似的内容: options.allowedSwipeDirections = MDCSwipeDirectionLeft | MDCSwipeDirectionRight; _allowedSwipeDirections = MDCSwipeDirectionLeft | MDCSwipeDirectionRight; 我甚至不知道这类变量的名称,但我已经在Swift中尝试了以下内容: options.al

我目前使用的cocoapod是用objective c编写的。 在示例中,它们显示了类似的内容:

options.allowedSwipeDirections = MDCSwipeDirectionLeft | MDCSwipeDirectionRight;
_allowedSwipeDirections = MDCSwipeDirectionLeft | MDCSwipeDirectionRight;
我甚至不知道这类变量的名称,但我已经在Swift中尝试了以下内容:

options.allowedSwipeDirections = MDCSwipeDirection.Left | MDCSwipeDirection.Right
options.allowedSwipeDirections = [MDCSwipeDirection.Left, MDCSwipeDirection.Right]
但是编译器说,
No'|候选程序生成预期的上下文结果类型“MDCSwipeDirection”

在斯威夫特我该怎么做

编辑:

看起来这不是一个选项开始,正如一些答案中所述,她的声明是:

/*!
 * 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;
它是这样使用的:

options.allowedSwipeDirections = MDCSwipeDirectionLeft | MDCSwipeDirectionRight;
_allowedSwipeDirections = MDCSwipeDirectionLeft | MDCSwipeDirectionRight;

我将假定此
MDCSwipeDirection
是符合的结构,例如,沿

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

    static let Left = SomeOptionSetType(rawValue:1)
    static let Right = SomeOptionSetType(rawValue:2)
}
在这种情况下,只需使用类似于列表的数组即可访问多个“案例”(静态属性;选项):

var myChoices : SomeOptionSetType = [.Left, .Right]
适用于你的情况,特别是

options.allowedSwipeDirections = [.Left, .Right]

由于我们可以假设
allowedSwipeDirections
属性的类型为
MDCSwipeDirection
,因此在对选项数组样式进行分组时可以忽略此类型。

您可以将其设置为Swift中的数组:

options.allowedSwipeDirections = MDCSwipeDirection.Left | MDCSwipeDirection.Right
options.allowedSwipeDirections = [MDCSwipeDirection.Left, MDCSwipeDirection.Right]
或速记:

options.allowedSwipeDirections = [.Left, .Right]
您需要一个选项集

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

查看选项集

MDCSwipeDirection
(不幸的是)是在Objective-C中定义的 作为
NS\u枚举
而不是作为
NS\u选项

typedef NS_ENUM(NSInteger, MDCSwipeDirection) {
    MDCSwipeDirectionNone = 1,
    MDCSwipeDirectionLeft = 2,
    MDCSwipeDirectionRight = 4,
    MDCSwipeDirectionUp = 8,
    MDCSwipeDirectionDown = 16
};
因此,它作为简单的
枚举导入Swift,而不是
作为
选项设置类型

public enum MDCSwipeDirection : Int {

    case None = 1
    case Left = 2
    case Right = 4
    case Up = 8
    case Down = 16
}
因此,对于
enum Int
转换:

let allowedSwipeDirections =  MDCSwipeDirection(rawValue: MDCSwipeDirection.Left.rawValue | MDCSwipeDirection.Right.rawValue)!
请注意,强制展开不能失败,请参见示例 :

。。。Swift 1.2现在允许使用 任意原始值(基础整数类型),如果 枚举是从
NS\u枚举
定义导入的。


如果将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 ]

试着像
[.Left、.Right]
那样使用它,我已经删除了我以前的评论。MDCSwipeDirection似乎来自于它在ObjC中定义为NS_枚举而不是NS_选项的位置。@Martinar实际上它是一个fork:但唯一的区别应该是向上滑动和向下滑动的可能性我不是向下投票人,但这给了我
上下文类型“MDCSwipeDirection”不能与数组文字一起使用,正如前面指出的那样。在这种情况下,
NS_ENUM
将作为
Int
类型的枚举导入Swift,其中cases
.Left
(rawvalue 0)和
.Right
(rawvalue 1)。如果是这种情况,我不知道如何允许多个允许的刷卡案例(使用
选项settype
一致类型)和
选项。允许的刷卡方向只需
。左
。右
,但不能同时使用。