2

In my app i want to create html file from arrays, can anyone know how this can be achieved?

Thanks in advance!

1 Answer 1

10

this is acutally quite easy:

You can start with an NSMutableString and build up a large String which you can convert later in a file:

NSArray *yourArray = [NSArray arrayWithObjects: @"green", @"blue", @"red", nil];

NSMutableString *htmlText = [[NSMutableString alloc] init];
[htmlText appendFormat: @"<html><head><title>TEST</title></head>"];
[htmlText appendFormat: @"<body>"];

[htmlText appendFormat: @"<ul>"];
for ( NSString *tmpText in yourArray] )
{
  [htmlText appendFormat: @"<li>%@</li>", tmpText];
}
[htmlText appendFormat: @"</ul>"];

[htmlText appendFormat: @"</body>"];
[htmlText appendFormat: @"</html>"];

// create the file

NSError *error = nil;

[htmlText writeToFile:yourPath atomically:YES encoding: NSUTF8StringEncoding error:&error];

if ( error )
{
  // do some stuff
}
Sign up to request clarification or add additional context in comments.

1 Comment

Makes no sense to me to use String. Isn't there a builder propertly?

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.