2017-06-14 97 views
0

所以我在的OBJ-C超這個方法:對象 - 以Swift3類返回方法

+ (Class<Executor>)executorClass 

我需要重寫它在我Swift3子類。我的嘗試:

override class func executorClass() -> AnyClass // not match 

override class func executorClass() -> Executor.Type // not match 

override class func executorClass() -> AnyObject.Type // not match 

override class func executorClass() -> Any.Type // obviously not match 

之前在Swift2下面的代碼工作:

override class func executorClass() -> AnyObject.Type 
+0

如果'AnyObject.Type'在Swift2中工作,那麼'AnyClass'應該與Swift3匹配。這是[Apple Reference](https://developer.apple.com/documentation/swift/anyclass)。 – nayem

+0

@nayem實際上不起作用。我將嘗試創建一個僅包含2個類的新項目,以檢查是否由於我的項目環境,某些副作用或其他原因。 –

+1

好'Class'沒有任何輕量級的通用佔位符,所以'Class '不合法。假設'executorClass'只是返回一個'Class',覆蓋類func的executorClass() - > AnyClass'應該可以正常工作。 – Hamish

回答

0

感謝@Harnish

中的OBJ-C法Class<Executor>是不合法的,但仍編譯。刪除<Executor>解決了編譯問題。又名:

// Obj-C 
+ (Class)executorClass 

// Swift 
override class func executorClass() -> AnyClass // good 

編輯:信貸@Sulthan

+ (Class<Executor>)executorClass 

上面的代碼是完全合法的,如果Executor了協議。但在我的情況下,這是另一類。所以,我猜,這是問題的根源。

+1

Obj-C'< ... >'混合協議和泛型的語法,它可能有點混亂。我會假設'Executor'是一個協議。 – Sulthan