大佬教程收集整理的这篇文章主要介绍了ios – Swift相当于为属性设置多个值,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
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;
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,请注明来意。