简体   繁体   English

从项目中提取NSDictionary

[英]Extracting NSDictionary from a project

I have a NSDictionary in a project that was created over time and i need to take all the data from it from this project into another project. 我在一个随时间推移创建的项目中有一个NSDictionary ,我需要将其中的所有数据从该项目中提取到另一个项目中。

Any idea how can i achive it without adding it one by one? 不知道我如何能不一一添加就实现它吗?

I thought about extracting everything into arrays and to pass arrays but its not really a good option 我考虑过将所有内容提取到数组中并传递数组,但这并不是一个好选择

Probaly not the best solution but it will work fine. 可能不是最好的解决方案,但可以正常工作。

You can convert the dictionary in to a string using the following steps. 您可以使用以下步骤将字典转换成字符串。 Then you log the string and copy it. 然后,您记录字符串并复制它。 After that you can do a reverse of these steps in your new project. 之后,您可以在新项目中执行相反的步骤。

Project 1 项目1

NSDictionary *dictionary = [NSDictionary alloc]init];
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary options:kNilOptions error:&error];
NSString *dictionaryString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(@"%@",dictionaryString); #Copy this string from the debugger

Project 2 项目二

NSError *error;
NSString *dictionaryString = @"PASTE_DEBUGGER_STRING";
NSData *jsonData = [dictionaryString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];

I would write it to a file and then import it in your new app. 我会将其写入文件,然后将其导入您的新应用中。 First you would ran the method in your app, and then take the file from the documents folder in iTunes. 首先,您将在应用程序中运行该方法,然后从iTunes中的documents文件夹中获取文件。 Add it to the documents folder in your new app and add the reading code. 将其添加到新应用程序的documents文件夹中,并添加阅读代码。

To save it: 要保存它:

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

BOOL success = [myDictionary writeToFile:stringsPlistPath atomically:YES];

if (success) {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"Your configuration file is in the Documents folder of the app which is accessible via iTunes." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
    [alert show];
} else {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"There was a problem creating the configuration file. Please try again." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
    [alert show];
}

To read it: 阅读:

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

NSDictionary *loadedDictionary = [NSDictionary dictionaryWithContentsOfFile:stringsPlistPath];

This will leave your file there. 这会将您的文件保留在那里。 If you wanted to delete it: 如果要删除它:

NSFileManager *fileManager = [NSFileManager defaultManager];

NSError *error;
BOOL success = [fileManager removeItemAtPath:stringsPlistPath error:&error];

if (success) {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"The configuration file was successfully loaded and has now been removed." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
    [alert show];
}
else
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"The configuration file was successfully loaded but could not be removed. You will need to remove it manually." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
    [alert show];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM