7

I have an array composed by other array:

example my array with two arrays inside:

myArray = [(element1, element2, element3)],[(element4,element5,element6)] this is only an example to show that myArray have two arrays (these elements are string)

now I want write in a txt file these elements in this way:

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

what is the code to create this string to write in txt file?

1 Answer 1

16
    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];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.