Checking if a .nib or .xib file exists

后端 未结 3 1856
走了就别回头了
走了就别回头了 2020-12-14 07:03

What\'s the best way to check if a Nib or Xib file exists before trying to load it using initWithNibName:bundle: or similar?

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-14 07:28

    There are two solutions I see here.

    You could just call initWithNibName:bundle: and catch an exception if it fails (I like this idea, it feels robust). You will probably want to verify that the exception is in fact a "file not found" exception rather than, say, an "out of memory" exception.

    Alternatively, you could check the existence of the nib first, using NSBundle's pathForResource:ofType:, which returns nil for files that don't exist.

提交回复
热议问题