gpt4 book ai didi

ios - 使用 NSMutableDictionary 生成 plist

转载 作者:行者123 更新时间:2023-11-28 22:09:34 24 4
gpt4 key购买 nike

我在从 NSMutableDictionnary 生成 plist 文件时遇到问题。好像不行我在 Stackoverflow 上看过很多例子。我从 plist 生成 Dictionary 但不是从 Dictionary 生成 plist。

这是我的代码:

NSArray *keys = [NSArray arrayWithObjects:@"id", @"name", nil];
NSArray *object = [NSArray arrayWithObjects:@"Value id", @"Value name", nil];
NSMutableDictionary *projectData = [NSDictionary dictionaryWithObject:object forKey:keys];

NSFileManager* fileManager = [NSFileManager defaultManager];


for (id key in projectData)
{
NSLog(@"projectData = key : %@, value : %@", key, [projectData objectForKey:key]);
}


NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *plistPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"data.plist"];
NSLog(@"%@", plistPath);
//write
[projectData writeToFile:plistPath atomically:YES];
NSMutableDictionary *newDictionnary;

//read
if ((plistPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"data.plist"])) {

if ([fileManager isWritableFileAtPath:plistPath]) {
[projectData writeToFile:plistPath atomically:YES];
newDictionnary = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath];
//NSLog(@"%@", plistPath);
}
}

else {
NSLog(@"File not found");
}
for (id key in newDictionnary)
{
NSLog(@"New dictionnary = key : %@, value : %@", key, [newDictionnary objectForKey:key]);
}

感谢您的帮助:)

最佳答案

请注意:您的 paths 变量从未被使用过。

您不能直接写入您的包目录。您必须写入文档文件夹、缓存文件夹或临时文件夹。

[[NSBundle mainBundle] bundlePath] 替换为 paths[0]

关于ios - 使用 NSMutableDictionary 生成 plist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23243768/

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