在我的应用程序中,我想从数组创建html文件,有谁知道这是如何实现的?
提前谢谢!

最佳答案

这非常简单:
可以从NSMutableString开始构建一个大字符串,稍后可以在文件中进行转换:

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
}

07-24 19:18