ios系列教程之常見開發Tips – iPhone手機開發技術文章 iPhone軟體開發教學課程

開發技巧和常見錯誤匯總,不定期更新中,也歡迎大傢總結跟帖

- (void) dealloc 
{
	//  [super dealloc] 千萬不要畫蛇添足    
	[[NSNotificationCenter defaultCenter] removeObserver:self]; 
}

下面是大傢常見的兩種定義優先選擇第二種

 #define PI 3.14159  //隻預處理時進行符號替換,不推薦
 const doulbe Pi=3.14159 //安全性高,和效率上推薦

Foo.h

extern NSString *constXXFooDidBarNotification;

Foo.m

NSString *const XXFooDidBarNotification =@"XXFooDidBarNotification”;

上面是引用的是 https://nshipster.com/nsnotification-and-nsnotificationcenter/中的一段聲明

有些童鞋不太理解const 修飾的用法 嘗嘗寫成

constNSString *XXFooDidBarNotification =@"XXFooDidBarNotification”;

詳情可以參看 :https://walkingsmarts.com/correct-way-of-defining-constants-in-objective-c/

之前一直用NSTimer ,這兩天再做性能優化的時候 發現瞭問題,用到NSTimer的ViewControll 一直沒有釋放. 後來跟蹤發現瞭問題.

[scheduledTimerWithTimeInterval :target:selector:userInfo:repeats:]

當target 為self (VC)的時候 因為Timer 強引用瞭self,
所以當你的

[ timer invalidate ]

寫到 dealloc 的時候 是永遠無法釋放當前VC的

蘋果官方文檔已經給出瞭

>
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 進行替換吧

有沒有用過下面的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]); 
  ... 
}
    在代碼中加註釋時,如果以 // TODO: 或 // FIXME: 或者 // !!!: 或 // ???: 開頭的話,此註釋會出現在方法的下列列表裡,方便日後跟蹤。利用Xcode4中的自定義代碼片段,可以用一個快捷鍵插入一個預先定義好的註釋片段

    在分辨率比較低的時候,iPad和iPhone模擬器會自動縮小,如果希望保持原始大小,可以修改 /Developer/Platforms/iPhoneSimulator.platform/Developer/Application/iOS Simulator/Contents/Info.plist,添加一個Application UI Presentaion Mode,設成All surpressed,則運行模擬器時自動隱藏菜單條和dock。在此目錄下的
    Resources/Devices/iPad.deviceinfo/Info.plist 中的chromeImageFile,把值設成空,可以去掉模擬器的邊框。

    這是昨天晚上在slack內討論的話題有必要加進來正好之前唐巧微博分享過的有興趣的可以去
    https://github.com/raywenderlich/objective-c-style-guide


    W 本站文章如果沒有特殊說明,均為原創,轉載請以鏈接方式註明本文地址:https://hufeng825.github.com/2014/01/21/ios36/

發佈留言