检查.nib或.xib文件是否存在 [英] Checking if a .nib or .xib file exists
问题描述
在尝试使用 initWithNibName:bundle:
或类似文件加载Nib或Xib文件之前,检查Nib或Xib文件是否存在的最佳方法是什么?
What's the best way to check if a Nib or Xib file exists before trying to load it using initWithNibName:bundle:
or similar?
推荐答案
宏
Macro
#define AssertFileExists(path) NSAssert([[NSFileManager defaultManager] fileExistsAtPath:path], @"Cannot find the file: %@", path)
#define AssertNibExists(file_name_string) AssertFileExists([[NSBundle mainBundle] pathForResource:file_name_string ofType:@"nib"])
以下是一组宏,您可以在尝试加载<$ c $之前调用它们c> .xib 或 .nib
,它们将帮助识别丢失的文件,并发出有关缺失内容的有用信息。
Here are a set of macros that you can call before you try an load a .xib
or .nib
, they will help identify missing files and spit out useful message about what exactly is missing.
使用的实际解决方案是:
The actual solution used was:
if([[NSBundle mainBundle] pathForResource:fileName ofType:@"nib"] != nil)
{
//file found
...
}
请注意,文档声明 ofType:
应该是文件的扩展名。但是,即使您使用的是.xib,也需要传递@nib,否则您将获得假阴性。
Please note, the documentation states that ofType:
should be the extension of the file. However even if you are using .xib you need to pass `@"nib" or you will get a false-negative.
这篇关于检查.nib或.xib文件是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!