2016-03-23 10 views
1

現在「swift programming language 2.1」を読んでおり、ダウンキャストキーワード「as」の使い方を学んでいます。本では、3つの異なる形式があります:as、as?そして!私はいつ使用するのか理解していますか?そして! 「いつも」をいつ使うべきかを理解するのに問題があります。誰にも何か提案がありますか?提案に付随するコードがあればさらに良いでしょう。助けをあらかじめありがとう!スワイフで3種類のキーワード「as」を使用する場合

+0

ここにもう1:http://stackoverflow.com/questions/29674986/as-vs-as-operator-in-xcode-6-3-in-swift。 –

答えて

0

いくつかのコード例があります。アップキャストにはasを使用できます。参照コードの例はarticleのものです。

class Animal {} 
class Dog: Animal {} 

let a: Animal = Dog() 
a as Dog  // now raises the error: "'Animal is not convertible to 'Dog'; 
       // ... did you mean to use 'as!' to force downcast?" 

a as! Dog  // forced downcast is allowed 

let d = Dog() 
d as Animal  // upcast succeeds 
+1

@MartinR thxはすでにそれを修正しています –

関連する問題