iphone – 检查.nib或.xib文件是否存在

前端之家收集整理的这篇文章主要介绍了iphone – 检查.nib或.xib文件是否存在前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在尝试使用initWithNibName:bundle或类似文件加载之前,检查Nib或Xib文件是否存在的最佳方式是什么?

解决方法

#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"])

以下是您尝试加载.xib或.nib之前可以调用的一组宏,它们将帮助识别丢失的文件,并发出有关丢失的信息。

使用的实际解决方案是:

if([[NSBundle mainBundle] pathForResource:fileName ofType:@"nib"] != nil) 
{
//file found
...
}

请注意,该文档说明ofType:应该是该文件的扩展名。不过即使你使用的是.xib,你需要传递“@”nib“或者你会得到一个假阴性。

猜你在找的Xcode相关文章