對某個物件賦值時出現這個錯誤,例如:
tfContent.text = bodyText;
打開 NSZombieEnabled 後,console 會給出更明確的訊息:
***-[CFString _isNaturallyRTL]: message sent to deallocated instance 0x52cc690
意思是 _isNaturallyRTL 被送給了一個已釋放的物件。從程式碼看,嫌疑對象只有 tfContent 與 bodyText 兩個,於是把兩者的位址印出來比對:
NSLog(@"tfContent:0x%x", (int) tfContent); NSLog(@"bodytext:0x%x", (int) bodyText); // tfContent: 0x52cf160 // bodytext: 0x52cc690 ← 與錯誤訊息中的位址一致
確認是 bodyText 被提早釋放,在合適的位置對它 retain 即可解決。ARC 專案則應檢查是否有 unsafe_unretained 或 assign 修飾的物件屬性造成懸空指標。
重啟設備即可。
實際原因是該標頭檔沒有被加入 target。選取對應的 .m 檔,點開工具列右側的 Show Utilities,在 Target Membership 中把勾去掉再重新勾上,相當於重新把該檔案加入 Build Phase。
Xcode 3.x 可以透過 Create iPad Version 選單完成,但 Xcode 4.x 找不到這個入口。替代做法有兩步。
第一步、修改 xib 原始碼。xib 本質上就是 XML。在 Project Navigator 中對 xib 按右鍵,選擇 Open As → Source Code,找到 com.apple.InterfaceBuilder3.CocoaTouch.XIB 這一行,改為 com.apple.InterfaceBuilder3.CocoaTouch.iPad.XIB(即加上 .iPad)。接著用搜尋取代功能,把檔案中所有 IBCocoaTouchFramework 替換成 IBIPadFramework,存檔。
第二步、調整視圖尺寸。對 xib 按右鍵選擇 Open As → Interface Builder – iOS,選取根視圖 UIView,在屬性面板把 Size 改為 Full iPad Screen。
兩種解法:開啟 Build Settings,找到 Compress PNG Files 並設為 No;或者選取該 png 檔,在 File Inspector 中把 File Type 從 PNG 改為 Icon。
Target → Build Settings,搜尋 prototype,把 Missing Function Prototypes 改為 NO。
把 Scheme 從 iOS Device 改為 iPhone 5.0 Simulator,或將 C/C++ 編譯器改為 LLVM GCC 4.2。另外,Core Plot 1.0 已不再支援舊的 armv6 CPU。
UIView 的 alpha 在 CALayer 層級其實叫 opacity,keyPath 要寫成 @"opacity"。
需要明確設定 majorTickLocations:
NSMutableArray *customTickLocations = [[NSMutableArray alloc] init];
for (int i = 0; i < 10; i++) {
[customTickLocations addObject:[NSNumber numberWithInt:i]];
}
xAxis.majorTickLocations = [NSSet setWithArray:customTickLocations];
需要在自訂 cell 中實作 layoutSubviews,手動把縮排量套到每個子視圖上:
- (void)layoutSubviews {
[super layoutSubviews];
float indentPoints = self.indentationLevel * self.indentationWidth;
for (UIView *view in self.subviews) {
view.frame = CGRectMake(
view.frame.origin.x + indentPoints,
view.frame.origin.y,
view.frame.size.width,
view.frame.size.height);
}
}
這裡順帶一個提醒:直接遍歷 self.subviews 做位移,在 iOS 7 之後可能會與系統自動產生的內容視圖互搶位置,實務上更穩妥的做法是調整 contentView 的子層級,或在 iOS 8 以後直接使用 layoutMargins。