1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

swift2.0 Generics の優先順

Last updated at Posted at 2015-09-12

勉強会で教えていただいたので自分用にメモ。
環境は Xcode Version 7.0 beta (7A120f) です。

protocol Count {

    typealias CountType
    var count: CountType{ get }

    typealias BolType
    var bl: BolType{ get }

}

class testVC: UIViewController, Count {

    var num:    Int = 30
    var count:  Int {
        return num
    }

    var truue:  Bool = true
    var bl:     Bool {
        return truue
    }

    func f<T:Any>               (v:T) { print("Any              \(v)") }
    func f<T:UIView>            (v:T) { print("UIView           \(v)") }
    func f<T:Count>             (v:T) { print("Count            \(v)") }
    func f<T:UIViewController>  (v:T) { print("UIViewController \(v)") }
    func f<T:testVC>            (v:T) { print("testVC           \(v)") }
    
    // Where制約(EquatableとComparableとHashable)
    
    // Int(ComparableとHashable同時定義はエラー)
    func fI<T:Count>                              (v:T) { print("I ---        \(v)") }
    func fI<T:Count where T.CountType: Comparable>(v:T) { print("I Comparable \(v)") }
    func fI<T:Count where T.CountType: Equatable> (v:T) { print("I Equatable  \(v)") }

    func fJ<T:Count>                              (v:T) { print("J ---        \(v)") }
    func fJ<T:Count where T.CountType: Hashable>  (v:T) { print("J Hashable   \(v)") }
    func fJ<T:Count where T.CountType: Equatable> (v:T) { print("J Equatable  \(v)") }

    func fK<T:Count>                              (v:T) { print("K ---        \(v)") }
    func fK<T:Count where T.CountType: Equatable> (v:T) { print("K Equatable  \(v)") }

    // Bool(Comparableは定義できないのでそれのみはエラー)
    func fB<T:Count>                              (v:T) { print("B ---        \(v)") }
    func fB<T:Count where T.BolType: Hashable>    (v:T) { print("B Hashable   \(v)") }
    func fB<T:Count where T.BolType: Equatable>   (v:T) { print("B Equatable  \(v)") }
    func fB<T:Count where T.BolType: Comparable>  (v:T) { print("B Comparable \(v)") }

    func fC<T:Count>                              (v:T) { print("C ---        \(v)") }
    func fC<T:Count where T.BolType: Equatable>   (v:T) { print("C Equatable  \(v)") }
    func fC<T:Count where T.BolType: Comparable>  (v:T) { print("C Comparable \(v)") }

    func fD<T:Count>                              (v:T) { print("D ---        \(v)") }
    func fD<T:Count where T.BolType: Comparable>  (v:T) { print("D Comparable \(v)") }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let v1: UIView = self.view
        let v2: UIView = UIView()
     
        f(self)
        f(UIViewController())
        f(self.view)
        f(v1)
        f(v2)
        f()
        
        fI(self)
        fJ(self)
        fK(self)
        
        fB(self)
        fC(self)
        fD(self)
    }
}

実行結果。
where制約も問題なく動作確認できました。

testVC           <SymPlayer.testVC: 0x7f9961a4ab00>
UIViewController <UIViewController: 0x7f99605b3400>
Any              <UIView: 0x7f99605b0aa0; 省略>
UIView           <UIView: 0x7f99605b0aa0; 省略>
UIView           <UIView: 0x7f9960547520; 省略>
Any              ()
I Comparable <SymPlayer.testVC: 0x7f87c250cee0>
J Hashable   <SymPlayer.testVC: 0x7f87c250cee0>
K Equatable  <SymPlayer.testVC: 0x7f87c250cee0>
B Hashable   <SymPlayer.testVC: 0x7f87c250cee0>
C Equatable  <SymPlayer.testVC: 0x7f87c250cee0>
D ---        <SymPlayer.testVC: 0x7f87c250cee0>

self.view が Any になるのね。
UIViewController クラスでの定義が、

var view: UIView!

だからか?っということで、追加。

        let v3: UIView? = UIView()
        let v4: UIView! = UIView()
     
        f(v3)
        f(v4)
        f(v3 as UIView!)
        f(v4 as UIView)

で結果。

Any              Optional(<UIView: 0x7fc0195efe20; 省略>)
Any              <UIView: 0x7fc0195c9290; 省略>
Any              <UIView: 0x7fc0195efe20; 省略>
UIView           <UIView: 0x7fc0195c9290; 省略>

? はオプショナル型なので納得。
! も Any になるのね。

ということで、忘れないようにメモ。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?