2016-07-31 43 views
0

我正在學習protobuf,並且正在和alexeyxo/protobuf-swift玩。 有沒有方法將protobuf消息轉換爲它們擴展的類型?將Protobuf信息傳遞給它們的擴展類型

原型文件:

message Command_Login { 
    extend SessionCommand { 
     optional Command_Login ext = 1001; 
    } 
    optional string user_name = 1; 
    optional string password = 2; 
} 

這裏是swift代碼:

let commandContainerBuilder = CommandContainer.Builder() 
commandContainerBuilder.sessionCommand.append(commandLogin) 
// sessionCommand is an array of SessionCommand (of which Command_Login extends) 

錯誤:

Cannot convert value of type CommandLogin? to expected argument type SessionCommand

回答

1

抱歉,你誤解擴展。我說「對不起」,因爲這可能是我的錯 - 我設計了「擴展」功能,不幸的是,通過使用「擴展」這個詞,我迷惑了很多人。

你看,擴展與繼承無關。在你的例子中,你是而不是宣稱Command_LoginSessionCommand的任何一種子類。這很容易理解,如果我們轉向了一下週圍的聲明:

message Command_Login { 
    optional string user_name = 1; 
    optional string password = 2; 
} 

extend SessionCommand { 
    optional Command_Login ext = 1001; 
} 

以上是完全有效的,並完全等同於你的代碼,但有一個區別:在您的版本,擴展名是Command_Login.ext(因爲你聲明它嵌套在Command_Login內),但在我的版本中,名稱僅爲ext(在全球範圍內)。除命名空間外,它們的功能相同。

extend子句實際上做的是在SessionContext上聲明一個新字段,其中該字段的類型爲Command_Login。如果您碰巧在message塊內部放置了extend子句,則這僅適用於命名空間的目的,這很像在C++或Java中聲明類的靜態成員。