这里有另一个帖子关于第16章的这一部分但已关闭。

我不太确定在这里发生了什么。我理解变量accounttype是一种帐户,它设置为bitcoincount。

但我虽然那个.self suffix将比特币帐户metatype分配给consumentype。

Xcode表示,Type BitcoInAccount的常量帐户OS,但我添加了一个额外的构造变量,以便Bitcoin帐户被称为Bitcoin,我被告知该帐户没有名为BitCoin的成员。

所以我猜我的问题是,是一种继承协议的类型的metatype,只需协议?

@ kampai32 这是一个很棘手的是一个正确的,但一旦你明白在引擎盖之后实际发生了什么,这绝对都是有道理的。

account is of type BitcoinAccount since it uses the BitcoinAccount class to implement the Account protocol, so the following line of code returns 真的 in this case:

let isBitcoinAccount = account is BitcoinAccount 

The problem over here is that account has access 只要 to the BitcoinAccount class part which implements the Account protocol, so 只是 account.value works properly because of that. You can easily check this out by doing a straightforward cast of account to BitcoinAccount as follows:

let sameBitcoinAccount = account as BitcoinAccount

The above line of code doesn’t compile at all at this point since account is still of type Account under the hood. In order to fix this, simply perform a forced downcast of account to BitcoinAccount like this and you are good to go:

let account = accountType.init(initialAmount: 30) as! BitcoinAccount

如果您有机会,请告诉我如果您有任何其他问题或关于整件事的问题。谢谢!