How to check if a Storyboard exists in a specific NSBundle

江枫思渺然 提交于 2019-12-20 20:20:09

问题


I'm making an app that makes use of a framework that I'm creating.

The framework contains storyboards but in some cases the project making use of the framework will need to override the storyboard by providing a new one.

So my goal is to check if a storyboard with a given name exists in [NSBundle mainBundle], if not then I will get the base version from my framework.

I have tried getting the storyboard from the main bundle and checking for the result being nil but it throws an exception if it can't find it. So I then tried to catch the exception and then load the storyboard from the framework. This does work but it feels dirty and it could well hit the performance of my app.

I have also tried pathForResource in the bundle:

if([[NSBundle mainBundle] pathForResource:name ofType:@"storyboard"] != nil) {

}

But this never finds the storyboard.

Does anyone know of any other way that I can check if a storyboard exists in a specific bundle?


回答1:


So, I was nearly there with what I was already trying. As per a comment on my question, the compiled extension for a storyboard is "storyboardc". So I can check whether or not a storyboard exists in a bundle like so:

if([[NSBundle mainBundle] pathForResource:name ofType:@"storyboardc"] != nil) {
    //The storyboard exists
} else {
    //The storyboard isn't in the bundle, get it from the framework bundle.
}


来源:https://stackoverflow.com/questions/38590288/how-to-check-if-a-storyboard-exists-in-a-specific-nsbundle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!