2016-11-11 112 views
0

我試圖將我的應用程序遷移到Swift 3,但我有麻煩。擴展錯誤Swift 3 JSON

是奮鬥我的代碼段是:

extension JSON: Swift.BooleanType { 

//Optional bool 
public var bool: Bool? { 
    get { 
     switch self.type { 
     case .bool: 
      return self.object.boolValue 
     default: 
      return nil 
     } 
    } 
    set { 
     if newValue != nil { 
      self.object = NSNumber(value: newValue! as Bool) 
     } else { 
      self.object = NSNull() 
     } 
    } 
} 

在第一行就是Xcode中拋出一個錯誤:

extension JSON: Swift.BooleanType { 

錯誤說:Inheritance from non-protocol type 'BooleanType' (aka 'Bool')

有誰知道發生了什麼那裏?

+0

沒有任何反應,它會引發同樣的問題 –

回答

0

協議是Swift等價的Java接口。如果你以前從來沒有使用過接口,那麼它們就是沒有任何具體實現的類。它們的存在是爲了描述類的骨架(它應該有的屬性和方法名稱),而不實際實現它們,以便從接口繼承的其他類可以在後期充實它們。在Swift中,它們對於實現委託模式特別有用。

布爾不是一個協議。這是一個非常活躍,具有現有實施的呼吸混凝土類型。做你想做的事,你要麼需要重寫現有的get/set方法布爾類型或在official Apple Swift Blog.

3

最簡單的解決方案描述創建自己的布爾類型做法是什麼,

Bad : extension JSON: Swift.BooleanType {

Good : extension JSON: {

Reason : Admittedly, I am using this to modify SwiftyJSON which is a framework for processing JSON data. In doing some research it seems that they didn't allow BooleanType to allow for inheritance. The suggested means of dealing with this is simply to remove the type.

鏈接給出:https://forums.developer.apple.com/thread/53405