gpt4 book ai didi

ios - 将 NSString 添加到 .plist(属性列表)文件

转载 作者:行者123 更新时间:2023-11-29 03:33:44 25 4
gpt4 key购买 nike

我试图阅读许多问题/教程,但找不到答案!我正在尝试从 textField 中获取一个字符串并将其保存在我创建的 datas.plist 文件中(在默认键 Root 下)我试过这段代码:

//Save Name to plist
if([name.text length] > 0){
NSString *string = name.text;
[array addObject:string];
//plist path
NSString *path = [[NSBundle mainBundle] pathForResource:@"datas" ofType:@"plist"];
//save object
[array writeToFile: path atomically:YES];

}

其中数组是一个

@interface DetailViewController (){
NSMutableArray* array;
}

但是当我尝试保存时应用程序崩溃(我之前写的操作连接到一个按钮)给出这个错误:

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Cannot create an NSPersistentStoreCoordinator with a nil model'

我认为编写的代码是不正确的,因为我从不输入保存位置的 key (如@“Root”)或类似的东西。提前致谢

编辑:

我试过这段代码,但没有写任何东西:

- (void)addToMyPlist {
NSString *destPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
destPath = [destPath stringByAppendingPathComponent:@"dati"];

// If the file doesn't exist in the Documents Folder, copy it.
NSFileManager *fileManager = [NSFileManager defaultManager];

if (![fileManager fileExistsAtPath:destPath]) {
NSString *sourcePath = [[NSBundle mainBundle] pathForResource:@"dati" ofType:@"plist"];
[fileManager copyItemAtPath:sourcePath toPath:destPath error:nil];
}

// Load the Property List.
array = [[NSMutableArray alloc] initWithContentsOfFile:destPath];

//NSString *path = [[NSBundle mainBundle] pathForResource:@"dati" ofType:@"plist"];
NSString *string = self.name.text;

NSArray *values = [[NSArray alloc] initWithObjects:string, nil];
NSArray *keys = [[NSArray alloc] initWithObjects:@"Root", nil];
NSDictionary *dict = [[NSDictionary alloc] initWithObjects:values forKeys:keys];
[array addObject:dict];
[array writeToFile:destPath atomically:YES];
}

怎么了?

最佳答案

bundle 中的所有文件都是只读的。

您必须将 pList 移动到应用的私有(private)文档(或其他,有 3 个可用)文件夹中才能获得写入权限。

例如,将捆绑文件复制到 Documents 目录中:

if([name.text length] > 0){
NSString *string = name.text;
[array addObject:string];

NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
//Path to document directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

//Path to your datas.plist inside documents directory
NSString *documentsPath = [documentsDirectory stringByAppendingPathComponent:@"datas.plist"];

//If the file doesn't exists yet
if ([fileManager fileExistsAtPath:documentsPath] == NO) {
//Let's copy it in your Documents folder
NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"datas" ofType:@"plist"];

[fileManager copyItemAtPath:bundlePath toPath:documentsPath error:&error];
}

[array writeToFile: documentsPath atomically:YES];

}

关于ios - 将 NSString 添加到 .plist(属性列表)文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19448034/

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