开发者

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

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 writ开发者_运维问答e 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?


    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];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜