0

The code I have right now looks like this:

NSString *path = @"/Users/student/Desktop/conf.txt"

NSArray *myArray = [NSArray arrayWithObjects:@"hello",@"world",@"etc",nil];

[myArray writeToFile:path atomically:YES];

When I look at the text file it writes it ends up looking like this:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"     "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
    <string>hello</string>
     <string>world</string>
    <string>etc</string>
 </array>
 </plist>

I don't want to display it as an html, I just want a simple text document with the 3 lines saying, "hello world etc".

Does anybody know how I could fix this?

Thanks

2
  • 1
    It is not HTML, it is plist file format Commented Aug 1, 2012 at 13:20
  • Oh, I should have realized that. Any Idea of how to fix it? Commented Aug 1, 2012 at 13:23

1 Answer 1

3

If your array contains strings and you wat to write them to file one per line you can first create string with all your words and then write it to file:

NSString *outString = [myArray componentsJoinedByString:@"\n"];
NSError *error = nil;
[outString writeToFile: path atomically:YES encoding:encoding:NSUTF8Encoding error:&error];

But if you want to serialize your array for future use plist format may be more convenient

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks that worked great! But what do you mean by serialize? I'm still trying to pick up on all this stuff.
@Craig, I mean if later you want to read that array back and use again in application it will be more convenient to save and load it directly as array

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.