How to save Xcode application data

I have been looking for many days how to save data from my applications. I found something, but it was very difficult and poorly explained. I need, when I completely close my applications, all the data that I entered in the text box is still there when I open my applications again. I tried the tutorial, but it only allowed me to save about 8 text fields, and I need to save thousands, I start Objective-C and Xcode, so if someone wants to give me an answer, please make it very accurate.

+4
source share
3 answers

Well, I would suggest putting all the data from your text fields into an array and save this to a file, and then load it when you reopen the application.

The first thing you need is to save the file. This feature will create for you.

-(NSString*) saveFilePath{ NSString* path = [NSString stringWithFormat:@"%@%@", [[NSBundle mainBundle] resourcePath], @"myfilename.plist"]; return path;} 

Now that this is done, you need to create your save array. I hope you have thousands of text fields that are already set to some sort of array. If not, it will be a painful process, no matter how you solve it. But anyway ... (Here labelArray will be an array of all your text fields / labels, etc.)

 NSMutableArray* myArray = [[NSMutableArray alloc]init]; int i = 0; while(i < labelArray.count){ [myArray addObject: [labelArray objectAtIndex: i].text]; i ++; } [myArray writeToFile:[self saveFilePath] atomically:YES]; [myArray release]; 

And the download code will be something like lines

 NSMutableArray* myArray = [[NSMutableArray arrayWithContentsOfFile:[self saveFilePath]]retain]; 

Then you simply load the data back into your array of text fields.

Hope this helps.

+5
source

It looks like your application architecture may be unreasonable if you plan to save thousands of text field data in the fraction of a second you receive while your application closes. It would probably be better to save them, as the user enters data instead of waiting to save all the data at once.

+2
source

To get the path you are going to write (or read from!), You do the following:

NSString *writableDBPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"MyFile.extension"];

And then use a writeToFile: automically: "method from NSString or NSDictionary, etc. to write to the path.

0
source

Source: https://habr.com/ru/post/1341314/


All Articles