IOS: write a line in a txt file from an array

I have an array consisting of another array:

An example of my array with two arrays inside:

myArray = [(element1, element2, element3)], [(element4, element5, element6)] is just an example showing that myArray has two arrays (these elements are strings)

Now I want to write these elements to a txt file as follows:

element1#element2#element3;element4#element5#element6; 

What is the code to create this line for writing in a txt file?

+6
source share
1 answer
  NSMutableString *printString = [NSMutableString stringWithString:@""]; for(i=0;i<[myArray count];i++) { for (NSString element in [myArray objectAtIndex:i]) { [printString appendString:[NSString stringWithFormat:@"%@#",element] ]; } [printString appendString:@";"]; } //CREATE FILE NSError *error; // Create file manager //NSFileManager *fileMgr = [NSFileManager defaultManager]; NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"fileArray.txt"]; NSLog(@"string to write:%@",printString); // Write to the file [printString writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error]; 
+15
source

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


All Articles