在我的应用程序中,我想从数组创建 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
}
关于html - 如何在 objective-c 中创建 html 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5777441/