在我的应用程序中,我想从数组创建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
}