iphone - 更早地检测有问题的 XIB View
<p><p>我的 nib 名称中有一个拼写错误,后来在我推送到导航 Controller 时它在代码中爆炸了。弄清楚它并没有花太长时间,但我认为最好早点断言格式良好,以便更容易弄清楚。问题是它不是零,它只是不能从 Nib 正确地形成。在 initWithNib 之后是否有更好的断言或捕获来检查代码中的早期问题? </p>
<pre><code>//
// typo in nib name - I want to catch before it blows up inside of pushViewController to narrow the problem
//
ENPurchaseDetailView *purchaseView = [
initWithNibName:@"XibWithTypoInIt" bundle:nil];
assert(purchaseView != nil);// does not catch - it's not nil - just not well formed
// other code ...
// blows up with sigabort inside of pushViewController
[ pushViewController:purchaseView animated:YES];
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>创建一个基础 UIViewController 类,您可以从该类继承所有其他 VC。然后,您可以在基类中执行许多有用的断言,而不必将它们分散在整个代码中。这是我使用的一个</p>
<pre><code>- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self =
// never do this stuff in production code
#ifdef DEBUG
// make sure you didn't fat finger Xib name
if (nibNameOrNil)
{
NSString *path = [ pathForResource:nibNameOrNil ofType:@"nib"];
NSAssert(path, @"Nib %@ does not exist", nibNameOrNil);
}
// make sure there's no problems with the Xib
UINib *nib = ;
NSArray *instantiatedObjects = ;
NSAssert(instantiatedObjects && > 0, @"Not well formed Xib");
#endif return self;
}
</code></pre></p>
<p style="font-size: 20px;">关于iphone - 更早地检测有问题的 XIBView ,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/7486373/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/7486373/
</a>
</p>
页:
[1]