在 plist 文件中添加字典
Adding dictionary in plist file
我需要在我的 plist 中添加项目 (organizations.plist)
我的 plist 按以下方式组织:
我有根字典,在这本中,我还有其他字典...
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>1</key>
<dict>
<key>Address</key>
<string>Address</string>
<key>City</key>
<string>City</string>
<key>Description</key>
<string>Description</string>
<key>Name</key>
<string>name</string>
<key>Coordonates</key>
<string>40.781208, 35.219622</string>
</dict>
</dict>
</plist>
然后我尝试通过以下代码添加字典:
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"organizations" ofType:@"plist"];
NSMutableDictionary *plist = [NSMutableDictionary dictionaryWithContentsOfFile:filePath];
NSMutableDictionary *newItem = [NSMutableDictionary dictionary];
[newItem setValue:@"address" forKey:@"address"];
[newItem setValue:@"City" forKey:@"City"];
[newItem setValue:@"32.067487, 34.785156" forKey:@"Coordonates"];
[newItem setValue:@"Description" forKey:@"Description"];
[newItem setValue:@"Name" forKey:@"Name"];
NSArray * allKeys = [plist allKeys];
int count = [allKeys count] + 1;
NSString *strFromInt = [NSString stringWithFormat:@"%d",count];
[plist setObject:newItem forKey:strFromInt];
[plist writeToFile:filePath atomically:NO];
[plist release];
NSLog([plist description]);
日志很好,添加了 newItem
,但是如果我打开 xCode 中的 organizations.plist 文件,我发现没有添加任何内容
首先将您的 plist file
复制到 document directory
。
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES);
plistPath = [[paths lastObject] stringByAppendingPathComponent:@"organizations.plist"];
if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]){
NSString *bundle = [[NSBundle mainBundle] pathForResource:@"organizations" ofType:@"plist"];
BOOL sucesss = [[NSFileManager defaultManager] copyItemAtPath:bundle toPath:plistPath error:&error];
if (!sucesss) {
NSLog(@"Exception fail to copyItemAtPath : %@",[error description]);
}
}
然后当您需要写入文件时,您必须从 document directory
获取 plist
并需要在那里 update
。
NSMutableDictionary *plist = [NSMutableDictionary dictionaryWithContentsOfFile: plistPath];
NSMutableDictionary *newItem = [NSMutableDictionary dictionary];
[newItem setValue:@"address" forKey:@"address"];
[newItem setValue:@"City" forKey:@"City"];
[newItem setValue:@"32.067487, 34.785156" forKey:@"Coordonates"];
[newItem setValue:@"Description" forKey:@"Description"];
[newItem setValue:@"Name" forKey:@"Name"];
NSArray * allKeys = [plist allKeys];
int count = [allKeys count] + 1;
NSString *strFromInt = [NSString stringWithFormat:@"%d",count];
[plist setObject:newItem forKey:strFromInt];
[plist writeToFile:filePath atomically:NO];
[plist release];
NSLog([plist description]);
我需要在我的 plist 中添加项目 (organizations.plist) 我的 plist 按以下方式组织: 我有根字典,在这本中,我还有其他字典...
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>1</key>
<dict>
<key>Address</key>
<string>Address</string>
<key>City</key>
<string>City</string>
<key>Description</key>
<string>Description</string>
<key>Name</key>
<string>name</string>
<key>Coordonates</key>
<string>40.781208, 35.219622</string>
</dict>
</dict>
</plist>
然后我尝试通过以下代码添加字典:
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"organizations" ofType:@"plist"];
NSMutableDictionary *plist = [NSMutableDictionary dictionaryWithContentsOfFile:filePath];
NSMutableDictionary *newItem = [NSMutableDictionary dictionary];
[newItem setValue:@"address" forKey:@"address"];
[newItem setValue:@"City" forKey:@"City"];
[newItem setValue:@"32.067487, 34.785156" forKey:@"Coordonates"];
[newItem setValue:@"Description" forKey:@"Description"];
[newItem setValue:@"Name" forKey:@"Name"];
NSArray * allKeys = [plist allKeys];
int count = [allKeys count] + 1;
NSString *strFromInt = [NSString stringWithFormat:@"%d",count];
[plist setObject:newItem forKey:strFromInt];
[plist writeToFile:filePath atomically:NO];
[plist release];
NSLog([plist description]);
日志很好,添加了 newItem
,但是如果我打开 xCode 中的 organizations.plist 文件,我发现没有添加任何内容
首先将您的 plist file
复制到 document directory
。
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES);
plistPath = [[paths lastObject] stringByAppendingPathComponent:@"organizations.plist"];
if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]){
NSString *bundle = [[NSBundle mainBundle] pathForResource:@"organizations" ofType:@"plist"];
BOOL sucesss = [[NSFileManager defaultManager] copyItemAtPath:bundle toPath:plistPath error:&error];
if (!sucesss) {
NSLog(@"Exception fail to copyItemAtPath : %@",[error description]);
}
}
然后当您需要写入文件时,您必须从 document directory
获取 plist
并需要在那里 update
。
NSMutableDictionary *plist = [NSMutableDictionary dictionaryWithContentsOfFile: plistPath];
NSMutableDictionary *newItem = [NSMutableDictionary dictionary];
[newItem setValue:@"address" forKey:@"address"];
[newItem setValue:@"City" forKey:@"City"];
[newItem setValue:@"32.067487, 34.785156" forKey:@"Coordonates"];
[newItem setValue:@"Description" forKey:@"Description"];
[newItem setValue:@"Name" forKey:@"Name"];
NSArray * allKeys = [plist allKeys];
int count = [allKeys count] + 1;
NSString *strFromInt = [NSString stringWithFormat:@"%d",count];
[plist setObject:newItem forKey:strFromInt];
[plist writeToFile:filePath atomically:NO];
[plist release];
NSLog([plist description]);