我的笔尖名称中有一个拼写错误,当我推送到导航控制器时,它在代码中爆炸了。它没有花太长时间来搞清楚,但我认为最好先断言好形象,以便更容易弄明白。问题是它不是零,它只是不能正确地从笔尖形成自己。在initWithNib之后是否有更好的断言或捕获来检查代码中的问题?
//
// typo in nib name - I want to catch before it blows up inside of pushViewController to narrow the problem
//
ENPurchaseDetailView *purchaseView = [[ENPurchaseDetailView alloc]
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
[[self navigationController] pushViewController:purchaseView animated:YES];
答案 0 :(得分:2)
创建一个基础UIViewController类,您可以从中继承所有其他VC。然后,您可以在基类中执行许多有用的断言,而不必在整个代码中分散它们。这是我使用的一个
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]
// never do this stuff in production code
#ifdef DEBUG
// make sure you didn't fat finger Xib name
if (nibNameOrNil)
{
NSString *path = [[NSBundle mainBundle] pathForResource:nibNameOrNil ofType:@"nib"];
NSAssert(path, @"Nib %@ does not exist", nibNameOrNil);
}
// make sure there's no problems with the Xib
UINib *nib = [UINib nibWithNibName:nibNameOrNil bundle:nibBundleOrNil ];
NSArray *instantiatedObjects = [nib instantiateWithOwner:nil options:nil];
NSAssert(instantiatedObjects && [instantiatedObjects count] > 0, @"Not well formed Xib");
#endif return self;
}