gpt4 book ai didi

iphone - iOS 将字典从一个 plist 导入到另一个 plist

转载 作者:行者123 更新时间:2023-11-28 20:16:18 27 4
gpt4 key购买 nike

我正在尝试让我的应用程序从电子邮件中将设置导入到自身中。这将起作用的方式是用户将向自己发送一个 settings.properties 文件,这实际上是一个看起来像这样的 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>Viewer</key>
<array>
<string>username</string>
<string>password</string>
<string>http://www.example.com</string>
<string>/example</string>
<string>urlparam=whatever</string>
</array>
</dict>

我可以使用 NSLog 打开并正确读取文件,但我需要将 dictionary 导入到我的应用程序主 Data.plist 中而不覆盖已经存在的内容,然后从 Documents/Inbox 文件夹中删除导入的 plist。

任何线索都会很棒 :)

//编辑

我已经根据下面的评论更新了我的方法。它现在仅在文件 Data.plist 已存在于我的应用程序文档目录中时才有效。因此,如果它不存在,我需要创建它,然后添加到我的 .properties 文件中。

这是我目前使用的代码。

-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
if (url){

NSDictionary *openedDictionary = [NSDictionary dictionaryWithContentsOfURL:url];

// get paths from root direcory
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
// get documents path
NSString *documentsPath = [paths objectAtIndex:0];
// get the path to our Data/plist file
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"Data.plist"];
NSDictionary *originalDictionary = [NSDictionary dictionaryWithContentsOfFile:plistPath];

NSMutableDictionary *newDictionary = [originalDictionary mutableCopy];
for (NSString *key in openedDictionary) {
if (!newDictionary[key]) {
newDictionary[key] = openedDictionary[key];
}
}

[newDictionary writeToFile:plistPath atomically:YES];

}
NSError *error = nil;
if (![[NSFileManager defaultManager] removeItemAtURL:url error:&error]) {
NSLog(@"error while deleting: %@", [error localizedDescription]);
}
return YES;
}

最佳答案

首先,如果你的数据结构是错误的。你有字典而不是数组来设置,它更具可读性,你解析它会更简单,不会出错。

我建议将您的文件编辑成这样

<?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>username</key>
<string>username_value</string>
<key>password</key>
<string>password_value</string>
<key>url</key>
<string>www.example.com</string>
<key>url2</key>
<string>/example</string>
<key>urlparam</key>
<string>urlparam=whatever</string>
</dict>

将 plist 转换为字典非常简单。

NSData* plistData = [source dataUsingEncoding:NSUTF8StringEncoding];
NSString *error;
NSPropertyListFormat format;
NSDictionary* plist = [NSPropertyListSerialization propertyListFromData:plistData mutabilityOption:NSPropertyListImmutable format:&format errorDescription:&error];

您应该阅读更多关于 Serializing a Property List 的内容

转换 plist 后,比较值并进行适当的更改并将字典写入 plist 文件。

这是代码片段:

-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
if (url){

NSDictionary *openedDictionary = [NSDictionary dictionaryWithContentsOfURL:url];

// get paths from root direcory
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
// get documents path
NSString *documentsPath = [paths objectAtIndex:0];
// get the path to our Data/plist file
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"Data.plist"];
NSDictionary *originalDictionary = [NSDictionary dictionaryWithContentsOfFile:plistPath];

NSMutableDictionary *newDictionary = [originalDictionary mutableCopy];
for (NSString *key in openedDictionary) {
if (!newDictionary[key]) {
newDictionary[key] = openedDictionary[key];
}
}

[newDictionary writeToFile:plistPath atomically:YES];

}
NSError *error = nil;
if (![[NSFileManager defaultManager] removeItemAtURL:url error:&error]) {
NSLog(@"error while deleting: %@", [error localizedDescription]);
}
return YES;
}

关于iphone - iOS 将字典从一个 plist 导入到另一个 plist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17989471/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com