錯誤訊息
-[MasterViewController setRefreshControl:]: unrecognized selector sent to instance
原因分析
- 屬性宣告:
@property (nonatomic, strong) UIRefreshControl *refreshControl;
- 模擬器運行正常(Xcode 新版 SDK,預設自動合成
@synthesize refreshControl = _refreshControl;)
- 真機 iOS 版本較低(或編譯目標較低),未自動合成 setter/getter,且原始碼中缺少
@synthesize
解決方案
- 補上 @synthesize(最快修復):
@implementation MasterViewController
@synthesize refreshControl = _refreshControl;
// ...
@end
- 升級專案部署目標 至 iOS 6+,現代 Xcode 預設自動合成
- 檢查建置設定:
Apple LLVM - Language - Modules 啟用 Enable Modules (C and Objective-C)
最佳實踐
- 新專案統一使用
@property 且不寫 @synthesize,要求部署目標 iOS 7+
- 維護舊專案時,統一補全
@synthesize 或升級部署目標
- 真機測試必須覆蓋最低支援 iOS 版本