HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – Swift相当于为属性设置多个值大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我目前正在使用以客观c编写的 cocoapod.
在示例中,它们显示了类似的内容

options.allowedSwipeDirections = MDCSwipeDirectionLeft | MDCSwipeDirectionRight;

我甚至不知道调用这些变量是什么,但我已经在Swift中尝试了以下内容

options.allowedSwipeDirections = MDCSwipeDirection.Left | MDCSwipeDirection.Right

编译器说没有’|’候选者产生预期的上下文结果类型’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;

解决方法

@H_696_34@ 不幸的是,在Objective-C中定义了MDCSwipeDirection
作为NS_ENUM而不是NS_OPTIONS:

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

因此它作为一个简单的枚举导入到Swift而不是
作为OptionSetType:

public enum MDCSwipeDirection : Int {

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

因此,您必须使用rawValue来兼容enum< - >诠释
转换:

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

请注意,强制解包不能失败,例如,请参阅
How to determine if undocumented value for NS_ENUM with Swift 1.2

如果将Objective-C定义更改为

typedef NS_OPTIONS(NSInteger,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 ]

大佬总结

以上是大佬教程为你收集整理的ios – Swift相当于为属性设置多个值全部内容,希望文章能够帮你解决ios – Swift相当于为属性设置多个值所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。