bool 便是 signed char,在iphone64位是unsigned char。php
單例模式在c++和java都有,通常是須要一個不釋放的對象,在建立時須要注意多線程的問題,使用synchronized(self)加鎖,retainCount使用一個無限制大的數html
,須要重寫retain和autorelease,release等函數。java
屬性:c++
The @property is an Objective-C directive which declares the property. The "retain" in the parenthesis specifies that the setter should retain the input value.數據庫
在屬性的setter中會retain一下對象xcode
好比:緩存
@property (nonatomic, retain) NSString *s01;多線程
self.s01 = stringObject; 這個時候stringObject的retainCount會加1;app
若是用_s01 = stringObject則stringObject的retainCount不會加1。iview
OC中的nil是空對象,調用任一方法都不會致使崩潰,因此對象最後釋放的時候置爲nil能夠保證程序的穩定。
cocos2d的一些概念
有ccsence, ccdirector, cclayer, ccsprite
其中sence是場景,遊戲由各個場景構成,如第一關,第二關,還有中間的過渡場景;
director負責場景直接的切換,是一個單例模式;
layer至關於層,一個場景中能夠包含好幾層,就是有好幾個layer,每一個layer上面能夠放sprite精靈
sprite精靈,每一個控件都是精靈,是遊戲的最基本單位,至關於uiview,能夠給sprite添加action。
cocos2d中的地圖編輯器叫tileMap
一個經過wifi鏈接的遊戲例子gameKit http://www.cocoachina.com/bbs/read.php?tid-16732.html
IOS中的線程池
跟java中的ThreadPoolExecutor差很少,都是生成n個線程放在那裏,不必定立刻執行任務,在須要的時候從線程池中取出線程,加入任務,這樣節省了每次建立線程須要的資源。
IOS的線程池是使用NSOperationQueue實現,每一個任務是NSOperation,在NSOperationQueue中添加NSOperation,系統就會執行NSOperation的- (void)main函數的內容
例子代碼以下:
1 NSOperationQueue *threadPool = [[NSOperationQueue alloc] init]; 2 int maxThreadCount = 4; 3 [threadPool setMaxConcurrentOperationCount:maxThreadCount]; 4 for (int i=0; i<maxThreadCount; i++) { 5 //create thread 6 7 NSLog(@"start addding operation. task %d", i); 8 ThreadTask *task = [[ThreadTask alloc] init]; 9 [threadPool addOperation:task]; 10 11 }
其中的ThreadTask是NSOperation的子類,重寫了- (void)main內容
ThreadTask.h
#import <Foundation/Foundation.h> @interface ThreadTask : NSOperation @end
ThreadTask.m
#import "ThreadTask.h" @implementation ThreadTask - (void)main { NSLog(@"execute task."); } @end
NSString *text = @"this is test code.";
@"this is test code."是有效常量。
使用[UIImage imageName:@"']方法獲取的圖片在內存中由系統cache管理,在同一頁面中加入多個使用同一個圖片的UIImage對象不會增長內存。
好比:
UIImage *img01 = [UIImage imageName:@"001.png"];
UIImage *img02 = [UIImage imageName:@"001.png"];
UIImage *img03 = [UIImage imageName:@"001.png"];
只是佔用了一分內存,不是3分內存空間。
若是要將NSArray內容寫入到文件中,能夠用下面這個接口:
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile;
同時須要注意NSArray中的對象必須是Foundation的類,ex:NSString/NSData/NSUrl等
若是是自定義的類,調用WriteToFile會返回失敗結果。
這個跟Object的Copy是同樣的,NSArray *arr01 = [NSArray alloc] initWithObject:@"1",@"2",@"3",nil];
NSarray *arr02 = [arr01 copy];
這樣沒有問題。若是是自定義的類,調用copy則只是複製了指針,沒有建立新的對應,生成對應的空間。
遍歷隊列時若是要刪除隊列成員的時候須要當心,容易引發崩潰
好比:
1 for (NSString *str in arrayWords) { 2 if ([str isEqualToString:@"2"]) { 3 [arrayWords removeObject:str]; 4 } 5 }
這樣會引發崩潰,由於arrayWords的長度發生了變化,最後會找不到對應位置的成員對象。
解決這個問題能夠在刪除後當即跳出循環,或者跳出循環後再刪除成員對象。
NSUserDefault是緩存在內存中的,不是每次都從數據庫中讀取數據,因此查詢起來速度很快。調用synchronize方法後會同步內存和數據庫的內容。
原文以下:
At runtime, you use an NSUserDefaults object to read the defaults that your application uses from a user’s defaults database. NSUserDefaults caches the information to avoid having to open the user’s defaults database each time you need a default value. The synchronize method, which is automatically invoked at periodic intervals, keeps the in-memory cache in sync with a user’s defaults database.
xcode調試APP的幾個技巧
http://www.cocoachina.com/newbie/env/2014/0526/8555.html
xcode工程中使用了boost庫致使產生不少warning的解決方法:
設置 Symbols Hidden by Default = YES
和 inline methods hidden = YES