61. 警告「add explicit braces to avoid dangling else」
所謂「危險的 else」,長得像這樣:
if (a == 10)
printf("TEN");
else
printf("NOT TEN");
a = 100;編譯器之所以抱怨,是因為它判斷不出你的語意:a = 100 這行到底屬於 if/else 之後的獨立敘述(不論 a 是否為 10 都要執行),還是你想在 else 分支裡才做的事?
如果是前者,正確寫法是補上大括號:
if (a == 10) {
printf("TEN");
} else {
printf("NOT TEN");
}
a = 100;如果是後者,則該把賦值放進 else 區塊:
if (a == 10) {
printf("TEN");
} else {
printf("NOT TEN");
a = 100;
}對 C/C++/Java 編譯器而言這只是風格問題,不影響編譯,而且編譯器實際會傾向第一種解讀。它之所以警告,是提醒你別讓程式碼留下歧義。想消掉這個警告,可以用 #pragma clang diagnostic ignored "-Wswitch" 這類方式,或把編譯選項裡的 Missing Braces and Parentheses 設為 NO。不過更推薦的作法永遠是把大括號補上。
62. iPad 模擬器看不到 Home 鍵
從 Xcode 4.3 起,為了換出更大的可用畫面空間,iPad 模擬器不再顯示 Home 鍵。你可以改用選單 Hardware → Home,或直接用快速鍵來代替。
63. No visible @interface for 'NSURL' declares the selector 'query'
這個方法在 iOS 6 已被捨棄,請改用 NSURL+Parameters 之類的替代方案。
64. Certificate identity 'iPhone Distribution' appears more than once
這是憑證重複導致的錯誤,得把鑰匙串裡重複的憑證刪掉才能編譯。惱人的是,重開 Xcode 之後那些刪掉的憑證又回來了——因為啟動時 Xcode 會把專案內的憑證重新匯入鑰匙串,所以單靠刪鑰匙串副本並不會有效。
這個 Xcode 老毛病讓不少人頭痛,但除了反覆(而且徒勞地)刪憑證,好像也沒別的辦法。其實也不能全怪 Xcode,它跟「iPhone 組態公用程式」兩邊都有關係。
Xcode 裡那些「殘留」憑證並不是以一般形式存在的。如果你裝了 iPhone 組態公用程式,這些憑證實際上位於 /Users/帳號/Library/MobileDevice/Applications/ 底下各個 .app 套件中——這些 .app 正是「iPhone 組態公用程式」的應用程式清單裡匯入的項目。用 Finder 執行「顯示套件內容」就能看到,其中名為 embedded.mobileprovision 的檔案就是重複的殘留憑證。你可以逐一刪除這些 .app,或者直接把該目錄下的 .app 全部清掉(反正只要專案還在,隨時都能重新編出 .app 再匯入公用程式)。最後別忘了把 Organizer 裡重複的憑證也刪一次,然後重啟 Xcode。
65. Application Identifier 'com.ydtf.*' which doesn't match the current setting 'com.ydtf.dlt'
如你所見,這兩個 Application ID 明明是匹配的(* 就是萬用字元),但這個莫名其妙的錯誤會讓你永遠編不過。這同樣是 Xcode 的 bug,解法是先把 Code Signing 改成 Don't Code Sign 編一次,再改回正確的簽章設定重新編。
66. The identity used to sign the executable is no longer valid.
這通常是前面簽章問題連帶造成的 Archive 失敗,處理方式同第 65 條。
67. 在 iPad 上調整 presentModalViewController 彈出視視窗的大小
TestViewController *testVC = [[TestViewController alloc] initWithNibName:@"TestViewController" bundle:nil]; testVC.modalPresentationStyle = UIModalPresentationFormSheet; testVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; [self presentModalViewController:testVC animated:YES]; // 重點:一定要在 presentModalViewController 之後再設定 frame testVC.view.superview.frame = CGRectMake(0, 0, 649, 397); testVC.view.superview.center = self.view.center;
順序錯了就不會生效,請務必把 frame 設定放在 presentModalViewController: 之後。
68. 在 iPad 上自訂 ActionSheet 的按鈕與 Popover 箭頭方向
ActionSheet 在 iPad 上是以 Popover 形式呈現的,預設不顯示 cancelButton——SDK 用 Popover 以外的區域代替取消鍵,使用者點外面就等於按取消。如果你這樣初始化:
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:self
cancelButtonTitle:@"cancel"
destructiveButtonTitle:@"ok"
otherButtonTitles:nil];最後只會看到紅色的 destructiveButton。
若你非要顯示 cancelButton,可以改寫成:
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:@"cancel", @"ok", nil];
// sheet.cancelButtonIndex = 0; 千萬不要設定這個
sheet.destructiveButtonIndex = 1;指定 destructiveButtonIndex 之後,該鈕會顯示為紅色。但千萬別去設 cancelButtonIndex,因為在 iPad 上 cancelButton 會被直接移除。
另外,iPad 上 SDK 並未提供修改箭頭方向的 API,方向由系統自行判斷。但我們可以靠 showFromRect: 的第一個參數來「騙」它:
CGRect r = sender.bounds; r.size.width = 2 * self.view.bounds.size.width; r.origin.x = -self.view.bounds.size.width + sender.bounds.size.width / 2 + sender.frame.origin.x; [sheet showFromRect:r inView:sender animated:YES];
這樣原本朝左的箭頭就會變成朝上。原理其實很樸素:iOS 判斷彈出方向時,就是看哪一邊有「足夠」的空間。當我們把三個方向都堵死,它也只能老實從我們想要的那一側彈出。
69. ASIHTTPRequest 中 setShowAccurateProgress = YES 沒有效果
必須在 networkQueue.showAccurateProgress = YES 之前先設定 request.showAccurateProgress = YES,否則不會生效:
request.showAccurateProgress = YES; networkQueue.showAccurateProgress = YES; [networkQueue setSuspended:YES]; [networkQueue addOperation:request]; networkQueue.uploadProgressDelegate = self; [networkQueue go];
另外提醒,由於 CFNetwork 的一個 bug,資料量小於 128K 時無法追蹤精確的上傳/下載進度。
70. 如何讓 UIWebView 的背景透明?
在 Interface Builder 裡把 Background 設為 Clear Color 並不會真的讓它透明,你需要這兩行:
[webView setBackgroundColor:[UIColor clearColor]]; [webView setOpaque:NO];