您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 開發疑難雜症題(3):從 Zombie 錯誤到 CorePlot 自訂的十個解法

編輯:iOS 開發綜合

22. message sent to deallocated instance

對某個物件賦值時出現這個錯誤,例如:

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 修飾的物件屬性造成懸空指標。

23. putpkt: write failed: Broken pipe

重啟設備即可。

24. .h file not found

實際原因是該標頭檔沒有被加入 target。選取對應的 .m 檔,點開工具列右側的 Show Utilities,在 Target Membership 中把勾去掉再重新勾上,相當於重新把該檔案加入 Build Phase。

25. Xcode 4 中把 iPhone 版 xib 轉為 iPad 版

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。

26. icon dimensions (0 x 0) don't meet the size requirements

兩種解法:開啟 Build Settings,找到 Compress PNG Files 並設為 No;或者選取該 png 檔,在 File Inspector 中把 File Type 從 PNG 改為 Icon。

27. 警告:no previous prototype for function

Target → Build Settings,搜尋 prototype,把 Missing Function Prototypes 改為 NO。

28. CorePlot 編譯失敗(clang failed with exit code 1)

把 Scheme 從 iOS Device 改為 iPhone 5.0 Simulator,或將 C/C++ 編譯器改為 LLVM GCC 4.2。另外,Core Plot 1.0 已不再支援舊的 armv6 CPU。

29. 用 CABasicAnimation 改動 UIView 的 alpha 無效

UIView 的 alpha 在 CALayer 層級其實叫 opacity,keyPath 要寫成 @"opacity"。

30. CorePlot 自訂 Axis Label 後 Tick Mark 消失

需要明確設定 majorTickLocations:

NSMutableArray *customTickLocations = [[NSMutableArray alloc] init];
for (int i = 0; i < 10; i++) {
    [customTickLocations addObject:[NSNumber numberWithInt:i]];
}
xAxis.majorTickLocations = [NSSet setWithArray:customTickLocations];

31. 自訂的 UITableViewCell 中 indentationLevel 不生效

需要在自訂 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。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved