OK,今天Himi介紹遊戲存儲這一塊,在Android遊戲開發中Himi介紹了好幾種保存的方式和形式,那麼在iOS中也有幾種方式,通常經常使用的有如下四種形式:數據庫
1. NSKeyedArchiver(加密形式)數組
2. NSUserDefaultsapp
3. Write寫入方式函數
4. SQLite3學習
這裏Himi就再也不介紹SQlite數據庫了,Android遊戲存儲中我也介紹過SQLite如何保存數據,可是因爲這一塊不少童鞋沒有接觸過數據庫相關知識,因此即便講了也沒做用,那麼對於數據數據庫的童鞋們能夠參考其餘博文或書籍進行學習;測試
首先介紹第一種保存方式:NSKeyedArchiveratom
代碼很簡單就很少解釋了直接上代碼:加密
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
//=================NSKeyedArchiver========================
NSString *saveStr1 = @
"我是"
;
NSString *saveStr2 = @
"數據"
;
NSArray *array = [NSArray arrayWithObjects:saveStr1, saveStr2, nil];
//----Save
//這一句是將路徑和文件名合成文件完整路徑
NSString *Path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filename = [Path stringByAppendingPathComponent:@
"saveDatatest"
];
[NSKeyedArchiver archiveRootObject:array toFile:filename];
//用於測試是否已經保存了數據
saveStr1 = @
"hhhhhhiiii"
;
saveStr2 =@
"mmmmmmiiii"
;
//----Load
array = [NSKeyedUnarchiver unarchiveObjectWithFile: filename];
saveStr1 = [array objectAtIndex:0];
saveStr2 = [array objectAtIndex:1];
CCLOG(@
"str:%@"
,saveStr1);
CCLOG(@
"astr:%@"
,saveStr2);
|
第二種保存方式:NSUserDefaultsspa
代碼很簡單就很少解釋了直接上代碼:code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
//=================NSUserDefaults========================
NSString *saveStr1 = @
"我是"
;
NSString *saveStr2 = @
"數據"
;
NSArray *array = [NSArray arrayWithObjects:saveStr1, saveStr2, nil];
//Save
NSUserDefaults *saveDefaults = [NSUserDefaults standardUserDefaults];
[saveDefaults setObject:array forKey:@
"SaveKey"
];
//用於測試是否已經保存了數據
saveStr1 = @
"hhhhhhiiii"
;
saveStr2 =@
"mmmmmmiiii"
;
//---Load
array = [saveDefaults objectForKey:@
"SaveKey"
];
saveStr1 = [array objectAtIndex:0];
saveStr2 = [array objectAtIndex:1];
CCLOG(@
"str:%@"
,saveStr1);
CCLOG(@
"astr:%@"
,saveStr2);
|
第三種保存方式:Write寫入方式
代碼很簡單就很少解釋了直接上代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
//=================Write寫入方式========================
NSString *saveStr1 = @
"我是"
;
NSString *saveStr2 = @
"數據"
;
NSArray *array = [NSArray arrayWithObjects:saveStr1, saveStr2, nil];
//----Save
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
if
(!documentsDirectory) {
NSLog(@
"沒找到"
);
}
NSMutableArray *saveDataArray=nil;
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@
"Savedatas.plist"
];
[[NSArray arrayWithObjects:array,nil] writeToFile:appFile atomically:NO];
//用於測試是否已經保存了數據
saveStr1 = @
"hhhhhhiiii"
;
saveStr2 =@
"mmmmmmiiii"
;
//----Load
if
([[NSFileManager defaultManager] fileExistsAtPath:appFile]){
saveDataArray = [NSMutableArray arrayWithContentsOfFile:appFile];
}
else
{
saveDataArray = [NSMutableArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@
"Savedatas"
ofType:@
"plist"
]];
}
NSArray *strArray = [saveDataArray objectAtIndex:0];
saveStr1 = [strArray objectAtIndex:0];
saveStr2 = [strArray objectAtIndex:1];
CCLOG(@
"str:%@"
,saveStr1);
CCLOG(@
"astr:%@"
,saveStr2);
|
不論哪一種形式代碼都很容易理解,那麼以上三種保存方式代碼中都帶有以下兩行代碼;
1
2
|
saveStr1 = @
"hhhhhhiiii"
;
saveStr2 =@
"mmmmmmiiii"
;
|
我已經註釋上了,爲了驗證是否保存數據成功,若是沒有保存成功,最後打印的確定就是hhhhhhiiiiiiiii,mmmmiiiii啦~正常保存的話以下圖控制檯信息:
因爲本章介紹過於簡單,那麼下面Himi對Write寫入方式保存數據和讀取數據封裝了兩個方法這裏放出,給童鞋們行個方便:(其餘兩種你們封裝起來風容易了,這裏就不寫了);
封裝的函數以下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
//保存遊戲數據
//參數介紹:
// (NSMutableArray *)data :保存的數據
// (NSString *)fileName :存儲的文件名
-(
BOOL
) saveGameData:(NSMutableArray *)data saveFileName:(NSString *)fileName
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
if
(!documentsDirectory) {
NSLog(@
"Documents directory not found!"
);
return
NO;
}
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:fileName];
return
([data writeToFile:appFile atomically:YES]);
}
//讀取遊戲數據
//參數介紹:
// (NSString *)fileName :須要讀取數據的文件名
-(id) loadGameData:(NSString *)fileName
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:fileName];
NSMutableArray *myData = [[[NSMutableArray alloc] initWithContentsOfFile:appFile] autorelease];
return
myData;
}
|
使用代碼以下:
1
2
3
4
5
6
|
NSString *saveStr1 = @
"測試保存讀取"
;
NSString *saveStr2 = @
"兩個函數"
;
NSMutableArray *array = [NSMutableArray arrayWithObjects:saveStr1, saveStr2, nil];
[self saveGameData:array saveFileName:@
"Himi"
];
NSMutableArray*temp =(NSMutableArray*)[self loadGameData:@
"Himi"
];
CCLOG(@
"%@--%@"
,[temp objectAtIndex:0],[temp objectAtIndex:1]);
|
運行效果截圖以下:
這裏Himi要提醒你們兩點細節也是須要注意的地方:
1.對於取出數據的時候須要注意,例如以下代碼:
1
2
3
|
NSUserDefaults *saveDefaults = [NSUserDefaults standardUserDefaults];
NSMutableArray *arraySaveData =[saveDefaults objectForKey:@
"OhSaveData"
];
//NSMutableArray *arraySaveData=[NSMutableArray arrayWithArray:[saveDefaults objectForKey:@"OhSaveData"]];
|
第二句代碼是經過一個文件名獲取你存儲的數據,返回數據數組,可是!必定要注意這裏返回的數據數組是不可修改的!及時你將讀取的數據賦給一個可修改的數組中也同樣沒法修改其中的數據,因此若是你想將取出的數據進行修改那麼這裏須要要使用第三行代碼來獲取,這裏Himi將獲取出的數據數組首先copy給了可修改數組中,那麼此時你的可修改數組就能夠正常修改了!
2.修改已經的存儲文件;代碼以下:
1
2
|
NSUserDefaults *saveDefaults = [NSUserDefaults standardUserDefaults];
[saveDefaults setObject:arraySaveData forKey:@
"已經存在的文件名"
];
|
arraySaveData:表示新的數據數組;