[英]Setting bitmask enum declared in objective-c from swift
我正在嘗試在新項目中使用iOS的Parse SDK。 它具有帶有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中的連續枚舉重構為使用NS_ENUM
,並且將位字段枚舉重構為使用NS_OPTIONS
。
你應該改變
typedef enum {
//...
} PFLogInFields;
至
typedef NS_OPTIONS(NSInteger, PFLogInFields) {
//...
};
我和你有同樣的問題。 有關如何在Swift中設置PFLogInFields
, PFLogInFields
答案 。 它為我工作!
在Swift中,您必須為枚舉類型加上前綴。 我不確定這是否可以自動用於Objective-C導入,但是它可能:
logInViewController.fields = PFLogInFields.PFLogInFieldsTwitter | ...
如果將庫移植到Swift標准,則字段將已經期望PFLoginFields
並且枚舉項將以某種方式定義,以便您可以編寫
logInViewController.fields = .Twitter | .Facebook ...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.