1
编写代码时,有许多情况下必须将其视为运行时错误:alloc/init返回nil,未找到资源,[someClass canDoThis]返回NO -needed特征,其中YES将是自然的回答,...iPhone - 如何在运行时处理错误
对于所有这些情况下,我写了exitWithMessage
例程(即显示一个警告框),每类都有释放分配内存kill
方法。
所以......当在init
方法,你有这几样例外,我认为你可以这样做:
[self kill];
[OneClass exitWithFatalErrorMessage];
return nil;
- (void) exitWithFatalErrorMessage:(NSString*)message
{
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:NSLocalizedStringFromTable(@"Error" @"ErrorMessages", @"") message:message delegate:self cancelButtonTitle:NSLocalizedStringFromTable(@"Understood", @"ErrorMessages", @"") otherButtonTitles: nil];
[alert show];
[alert release];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
// stop the normal running of the app, there is a situation that would prevent it
}
- (void)kill
{
self.member = nil;
self.member2 = nil;
...
}
但是,这并不工作...不显示我的警报(在exitWithMessage其他任何地方使用比为init方法时工作正常。
你会如何处理这些情况?这是一块编码罚款的方式做到的?
如果是的话,为什么我的警惕不显示(我进入视图控制器的initWithCoder方法为例)?
当然......这只是一个问题的错误。 – Oliver 2012-02-07 10:32:55