2016年7月11日 星期一

Objective Static


Objective Static


靜態變數預設的初始值為0,且執行期間只會被初始一次,以下使用Static關鍵字實現方法呼叫次數

@interface Page: NSObject -(void) showTimes; @end @implementation Page -(void) showTimes { static int PageTimes; ++PageTimes; NSLog(@"showTimes Call Times : %i", PageTimes); } @end int main(int argc, const char * argv[]) { @autoreleasepool { Page *myPage = [Page new]; [myPage showTimes]; Page *myPage2 = [Page new]; [myPage2 showTimes]; Page *myPage3 = [Page new]; [myPage3 showTimes]; } return 0; }



沒有留言:

張貼留言