2011-04-19 46 views
4

我已經建立了如何從plist讀取BOOL,但是我不知道如何寫BOOL值,我原來讀的。從plist中讀取BOOL,用新值寫入它

我讀的BOOL埋在我的plist內。加載視圖時,我從plist中獲取一個包含視圖所有信息的字典(深度結束處的詳細信息視圖),BOOL位於該字典內。一旦將字典帶入應用程序,我可以更改字典中BOOL的值,但我不知道如何將它寫回到plist中的位置。

希望這是有道理的?

乾杯!

回答

11

寫的NSDictionary的屬性列表:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"FileName.plist"]; 

NSDictionary *myDict = [[NSDictionary alloc] init]; 
[myDict setObject:[NSNumber numberWithBool:YES] forKey:@"myKey"]; 
[myDict writeToFile:filePath atomically:YES]; 

閱讀布爾:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"FileName.plist"]; 

NSDictionary *myDict = [[NSDictionary alloc] initWithContentsOfFile:filePath]; 
BOOL myBool = [[myDict objectForKey:@"myKey"] boolValue]; 

你也可以把文件路徑的代碼到這個功能:

- (NSString *)getFilePath { 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"FileName.plist"]; 
return filePath; 
} 

並像這樣使用它:(供閱讀荷蘭國際集團)

NSDictionary *myDict = [[NSDictionary alloc] initWithContentsOfFile:[self getFilePath]]; 
BOOL myBool = [[myDict [email protected]"myKey"] boolValue]; 

這(寫)

NSDictionary *myDict = [[NSDictionary alloc] init]; 
[myDict setObject:[NSNumber numberWithBool:YES] forKey:@"myKey"]; 
[myDict writeToFile:[self getFilePath] atomically:YES]; 
+0

BOOL myBool = [[myDict objectForKey @「myKey」] boolValue]; 在objectForKey和@「myKey」之間缺少冒號 – 2014-09-19 22:12:55

2

對於寫BOOL到的.plist下面的代碼使用

NSMutableDictionary* myDict = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath]; 
[myDict setObject:[NSNumber numberWithBool:YES] forKey:@"LSUIElement"]; 
[myDict writeToFile:plistPath atomically:NO]; 

也有看post

+0

我知道如何做到這一點,但我一直認爲它不會記得我是的plist中的位置,而是會寫字典到plist的根? – Luke 2011-04-19 18:37:23

0

試試這個功能:

-(void) setStringToPropertyList:(NSString*) pList withKey:(NSString*)key andValue:(NSString*)val{ 
//the path. 
NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
NSString *plistPath = [rootPath stringByAppendingPathComponent:pList]; 
//read the dictionary. 
NSMutableDictionary *temp = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath]; 
//modify the desired key of the dictionary. 
[temp setValue:val forKey:key]; 
//rewrite the diccionary again. 
if([temp writeToFile: plistPath atomically:YES]){ 
    NSLog(@"setStringToPropertyList: pList updated"); 
}else{ 
    NSLog(@"setStringToPropertyList: error writing the file"); 
} 
//memory management 
if(!temp) [temp release]; 
} 

此功能允許你寫性能的pList,參數:

pList =文件的名稱

鍵=屬性

val的關鍵=屬性

希望它可以幫助的價值。