一.創(chuàng)建Plist文件
1.手動創(chuàng)建? ?手動創(chuàng)建這位同學(xué)寫的可以
2.代碼創(chuàng)建
//獲取本地沙盒路徑
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES); //獲取完整路徑
NSString *documentsPath = [path objectAtIndex:0];
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"MY_PropertyList.plist"];
NSMutableDictionary *usersDic = [[NSMutableDictionary alloc ] init]; //設(shè)置屬性值 [usersDic setObject:@"孫悟空" forKey:@"name"];
[usersDic setObject:@"sunwukong" forKey:@"password"];
//寫入文件
[usersDic writeToFile:plistPath atomically:YES];?
二峻堰、讀取Pilst文件
加載plist數(shù)據(jù)? 凡是參數(shù)名是file 辟汰,傳遞的都是文件的路徑
?NSString? *filePath = [[NSBundle mainBundle] pathForResource:@"shop" ofType:@"plist"];
? NSArray? *arr = [NSArrayarrayWithContentsOfFile:filePath];
一個 NSBundle對象 對應(yīng)一個資源包(圖片闭专、音頻缺虐、視頻、plist等文件)巧骚。
NSBundle的作用:用來訪問與之對應(yīng)的資源包內(nèi)部的文件蒿赢,可以用來獲取文件全路徑颁督。
項目中添加的資源都會添加到主資源包中,利用mainBundle關(guān)聯(lián)的就是項目的主資源包
三赚哗、常見問題
1.項目里邊的某個.m文件無法使用
? ? ?檢查:Build phases -> Compile Sources
2.項目中某個資源文件(如:音頻她紫、Plist文件等)無法使用
? ? 檢查:Build phases -> Copy Bundle Resources