exit(1)、abort()、assert(0);
abort: 這是默認(rèn)的程序結(jié)束函數(shù),這種方式可能會(huì)或可能不會(huì)以刷新與關(guān)閉打開的文件或刪除臨時(shí)文件,這與你的設(shè)計(jì)有關(guān).????????????????????????????
exit: 附加了關(guān)閉打開文件與返回狀態(tài)碼給執(zhí)行環(huán)境,并調(diào)用你用atexit注冊(cè)的返回函數(shù)?????????????????????????????????????????????????????????????????????????????????
assert(1) 為oc中的宏,只在debug模式下有用,當(dāng)條件成立時(shí),程序不會(huì)終止掉;當(dāng)條件不成立時(shí),程序終止。
警告:不要使用exit函數(shù),調(diào)用exit會(huì)讓用戶感覺程序崩潰了,不會(huì)有按Home鍵返回時(shí)的平滑過渡和動(dòng)畫效果;另外,使用exit可能會(huì)丟失數(shù)據(jù),因?yàn)檎{(diào)用exit并不會(huì)調(diào)用-applicationWillTerminate:方法和UIApplicationDelegate方法;
如果在開發(fā)或者測(cè)試中確實(shí)需要強(qiáng)行終止程序時(shí),推薦使用abort 函數(shù)和assert宏;
exit 方法
- (void)exitApplication{
?? AppDelegate *app = [UIApplication sharedApplication].delegate;
?? UIWindow*window = app.window;//動(dòng)畫
?? [UIView animateWithDuration:1.0f animations:^{
???? window.alpha = 0;??
??? window.frame = CGRectMake(0, window.bounds.size.width, 0, 0);
?????? }completion:^(BOOLfinished) {
?????? exit(0);}];
?? //exit(0);
}
abort 方法
- (void)exitApplication{
???? #pragma clang diagnostic push
???? #pragma clang diagnostic ignored "-Wundeclared-selector"
???? //運(yùn)行一個(gè)不存在的方法,退出界面更加圓滑
???? [self performSelector:@selector(notExistCall)];
???? abort();
???? #pragma clang diagnostic pop
}