【问题标题】:Setting bitmask enum declared in objective-c from swift从swift设置在objective-c中声明的位掩码枚举
【发布时间】:2014-08-25 10:01:22
【问题描述】:

我正在尝试在我的新项目中使用 Parse SDK for iOS。它具有带有 enum 属性的 viewController;

typedef enum {
    PFLogInFieldsNone = 0,
    PFLogInFieldsUsernameAndPassword = 1 << 0,
    PFLogInFieldsPasswordForgotten = 1 << 1,
    PFLogInFieldsLogInButton = 1 << 2,
    PFLogInFieldsFacebook = 1 << 3,
    PFLogInFieldsTwitter = 1 << 4,
    PFLogInFieldsSignUpButton = 1 << 5,
    PFLogInFieldsDismissButton = 1 << 6,

    PFLogInFieldsDefault = PFLogInFieldsUsernameAndPassword | PFLogInFieldsLogInButton |      PFLogInFieldsSignUpButton | PFLogInFieldsPasswordForgotten | PFLogInFieldsDismissButton
 } PFLogInFields;

根据 Objective-C 中的教程,我应该这样设置:

 [logInViewController setFields: PFLogInFieldsTwitter | PFLogInFieldsFacebook | PFLogInFieldsDismissButton];

我正在尝试以这种方式(使用 swift):

loginViewController.fields = PFLogInFieldsTwitter | PFLogInFieldsFacebook | PFLogInFieldsDismissButton

但我收到错误:“'PFLogInFields' 不能转换为 'Bool'”

那么,设置此类属性的正确方法是什么?

【问题讨论】:

    标签: objective-c enums swift parse-platform bit-masks


    【解决方案1】:

    Objective-C 中的连续枚举应重构为使用NS_ENUM,位域枚举应重构为使用NS_OPTIONS

    你应该改变

    typedef enum {
        //...
    } PFLogInFields;
    

    typedef NS_OPTIONS(NSInteger, PFLogInFields) {
        //...
    };
    

    【讨论】:

      【解决方案2】:

      我和你有同样的问题。请参阅 this answer 了解如何在 Swift 中设置 PFLogInFields。它对我有用!

      【讨论】:

      • 我刚刚添加了一个包含所有我需要的选项 PFLogInFieldsFull =PFLogInFieldsUsernameAndPassword | PFLogInFieldsLogInButton | PFLogInFieldsSignUpButton | PFLogInFieldsPasswordForgotten | PFLogInFieldsDismissButton | PFLogInFieldsFacebook 使其工作。但是您提供的答案肯定要好得多。谢谢。
      • @Vitaliy1 太棒了。 3AM 节目万岁!
      【解决方案3】:

      在 Swift 中,你必须在枚举前面加上类型。我不确定这是否适用于 Objective-C 导入,但它可能:

      logInViewController.fields = PFLogInFields.PFLogInFieldsTwitter | ...
      

      如果库被移植到 Swift 标准,字段应该已经期望 PFLoginFields 并且枚举项将以某种方式定义,以便您可以编写

      logInViewController.fields = .Twitter | .Facebook ...
      

      【讨论】:

        猜你喜欢
        • 2013-04-16
        • 2012-06-21
        • 2010-10-31
        • 1970-01-01
        • 2015-05-21
        • 2018-08-05
        • 2015-10-14
        • 2014-07-29
        • 1970-01-01
        相关资源
        最近更新 更多