2016-12-13 4 views
0

私はクラス階層内のオブジェクトのクラスを決定しようとしています。私は次の例のテストが失敗する理由を説明するために迷っています。Swiftクラスの型の決定

class BasicLocation {} 
     class AddressLocation : BasicLocation {} 
     class ContactLocation : BasicLocation {} 

     func mapView(_ mapView : MKMapView, viewFor: MKAnnotation) 
     ->MKAnnotationView?{ 
     if let test = viewFor as? BasicLocation { 
      let basicType = BasicLocation() 
      let a = type(of:test) 
      let b = type(of:basicType) 
      let c = type(of:test) 
      NSLog("a=\(a), type of a=\(type(of:a))") 
      NSLog("b=\(b), type of b=\(type(of:b))") 
      NSLog("c=\(c), type of b=\(type(of:c))") 
      if a == b { 
       NSLog("passed a and b") 
      } else { 
       NSLog("a and b do not match match") 
      } 
      if a == c { 
       NSLog("passed a and c") 
      } 

output 

>a=BasicLocation, type of a=BasicLocation.Type 
>b=BasicLocation, type of b=BasicLocation.Type 
>c=BasicLocation, type of b=BasicLocation.Type 
>a and b do not match match 
>a and c natch 
+0

ここにはどのような意見がありますか?設定したコードを見ることができないので、出力にどのような影響があるかわかりません。 –

+0

'test'とは何ですか? – holex

+0

これは知っておくと便利でしょう:http://stackoverflow.com/a/40388434/3141234 – Alexander

答えて

0

そのテストに合格した場合は停止し、...

if let test = viewFor as? BasicLocation 

を言ったら。 testがBasicLocationのインスタンスであることがわかったか、テストに合格しなかったでしょう。

あなたがやっていることはすべて馬鹿です。メタタイプ間の等価比較は未定義です。

+0

最初のテストでは、クラスが階層のメンバーであることのみが判別されます。問題となるのは、階層内のどのクラスかです。 – Stephen

+1

それからそれらのクラスについて聞いてください。私はあなたの質問に他の授業は見ませんので、私は私が見ることができるものに応じて答えました。 – matt

関連する問題