LoginSignup
3
3

More than 5 years have passed since last update.

Superviewはどこまでもチェーンできるのだろうか

Last updated at Posted at 2014-06-05

あるビューを上にたどっていくときに

aView.superview.superview.superview.superview.superview  ...

みたいなひどいコードを書いて落ちたりしないのかしら、という疑問。aViewがscrollviewの中に入っていて、そのscrollviewまで何個たどればいいのかわからない、とか。

で、まあこんなコードを書いてみて実行する

    int i=0;
    NSView *v = _myView.superview;

    do {
        NSLog(@"%@", [v description]);
        v = [v superview];
    } while (i++<10);

2014-06-05 16:01:42.106 Superview[6135:303]
2014-06-05 16:01:42.107 Superview[6135:303]
2014-06-05 16:01:42.107 Superview[6135:303]
2014-06-05 16:01:42.107 Superview[6135:303]
2014-06-05 16:01:42.107 Superview6135:303
2014-06-05 16:01:42.107 Superview6135:303
2014-06-05 16:01:42.107 Superview6135:303
2014-06-05 16:01:42.108 Superview6135:303
2014-06-05 16:01:42.108 Superview6135:303
2014-06-05 16:01:42.108 Superview6135:303
2014-06-05 16:01:42.108 Superview6135:303

nilに対する superviewメソッドの呼び出しでは特にエラーになることはないようだ。最初に書いたひどいコードもとくに問題はないみたい。

objective-cのこのへんがよく分からない。呼び出してもnilが返るばかりで特にエラーにならなかったり、割と厳密に「そんなセレクターねえよ」と怒られたり、その辺の基準が。

3
3
3

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
3
3