開發(fā)技巧和常見錯誤匯總,不定期更新中,也歡迎大家總結(jié)跟帖
1:ARC下NSNotificationCenter需要remove
- (void) dealloc
{
// [super dealloc] 千萬不要畫蛇添足
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
2:const or #define
下面是大家常見的兩種定義優(yōu)先選擇第二種
#define PI 3.14159 //只預(yù)處理時進行符號替換,不推薦
const doulbe Pi=3.14159 //安全性高,和效率上推薦
3:常量字符串聲明
Foo.h
extern NSString *constXXFooDidBarNotification;
Foo.m
NSString *const XXFooDidBarNotification =@"XXFooDidBarNotification”;
上面是引用的是http://nshipster.com/nsnotification-and-nsnotificationcenter/中的一段聲明
有些童鞋不太理解const 修飾的用法 嘗嘗寫成
constNSString *XXFooDidBarNotification =@"XXFooDidBarNotification”;
詳情可以參看 :http://walkingsmarts.com/correct-way-of-defining-constants-in-objective-c/
4:NSTimer invalidate 錯誤
之前一直用NSTimer ,這兩天再做性能優(yōu)化的時候 發(fā)現(xiàn)了問題,用到NSTimer的ViewControll 一直沒有釋放. 后來跟蹤發(fā)現(xiàn)了問題.
[scheduledTimerWithTimeInterval :target:selector:userInfo:repeats:]
當target 為self (VC)的時候 因為Timer 強引用了self, 所以當你的
[ timer invalidate ]
寫到 dealloc 的時候 是永遠無法釋放當前VC的
5:NSTimer并不是一個高精度的定時器
蘋果官方文檔已經(jīng)給出了
> A timer is not a real-time mechanism; it fires only when one of the run loop modes to which the timer has been added is running and able to check if the timer’s firing time has passed. Because of the various input sources a typical run loop manages, the effective resolution of the time interval for a timer is limited to on the order of 50-100 milliseconds. If a timer’s firing time occurs during a long callout or while the run loop is in a mode that is not monitoring the timer, the timer does not fire until the next time the run loop checks the timer. Therefore, the actual time at which the timer fires potentially can be a significant period of time after the scheduled firing time.
所以當你需要高精度的定時器的時候 請用dispatch_after進行替換吧
6:調(diào)試的技巧
有沒有用過下面的log輸出呢嘗試下吧
-(void) buttonPressed :(UIButton *)button
{
NSLog(@"Stack trace: %@", [NSThread callStackSymbols]);
NSLog(@"Current selector: %@", NSStringFromSelector(_cmd));
NSLog(@"Object class: %@", NSStringFromClass([self class]));
NSLog(@"Filename: %@", [[NSString stringWithUTF8String:__FILE__] lastPathComponent]);
...
}
7:注釋技巧
在代碼中加注釋時,如果以 // TODO: 或 // FIXME: 或者 // !!!: 或 // ???: 開頭的話,此注釋會出現(xiàn)在方法的下列列表里旁壮,方便日后跟蹤。
利用Xcode4中的自定義代碼片段非迹,可以用一個快捷鍵插入一個預(yù)先定義好的注釋片段
8:改變模擬器窗口的大小
在分辨率比較低的時候,iPad和iPhone模擬器會自動縮小纯趋,如果希望保持原始大小憎兽,可以修改 /Developer/Platforms/iPhoneSimulator.platform/Developer/Application/iOS Simulator/Contents/Info.plist,添加一個Application UI Presentaion Mode吵冒,設(shè)成All surpressed纯命,則運行模擬器時自動隱藏菜單條和dock。在此目錄下的 Resources/Devices/iPad.deviceinfo/Info.plist 中的chromeImageFile痹栖,把值設(shè)成空扎附,可以去掉模擬器的邊框。
9:enum or NS_ENUM
這是昨天晚上在slack內(nèi)討論的話題有必要加進來正好之前唐巧微博分享過的有興趣的可以去