gpt4 book ai didi

iphone - HTML文件创建

转载 作者:行者123 更新时间:2023-12-01 17:58:28 38 4
gpt4 key购买 nike

我尝试以编程方式创建HTML文件。但是生成的文件显示为空白文件。我不知道这个问题。

这是我的代码:

- (IBAction) createFileAction
{
NSLog(@"************* createFileAction *************");
NSMutableString *htmlString = [[NSMutableString alloc] init];
NSLog(@"%@",htmlString);
[htmlString appendString:@"<html><head><title></title><style type='text/css'>.style1{height: 27px;}#Text1{height: 19px;width: 210px;}.style2{width: 255px;}.style3{height: 27px;width: 255px;}#petValue{width: 206px;}#nameValue{width: 206px;}#dateValue{width: 209px;}"];
[htmlString appendString:@"#Text2{width: 215px;}#Text3{width: 210px;height: 22px;}#Text4{width: 209px;}.style8{width: 13px;}.style9{ width: 263px;}.style10{height: 27px;width: 263px;}"];
[htmlString appendString:@".style11{width: 418px;}.style12{width: 199px;}.style13{height: 27px; width: 199px;}.style14{height: 24px;}.style15{width: 410px;}</style> </head>"];
[htmlString appendString:@"<body>"];

NSString *originalString = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.makepartsfast.com/2012/09/4337/more-3d-printing-in-metals-ex-one-introduces-the-m-flex-3d-printing-system/"] encoding:NSUTF8StringEncoding error:nil];
NSScanner *scanner = [NSScanner scannerWithString:originalString];
NSString *extractedString = nil;


[scanner scanUpToString:@"<div class=\"postarea\">" intoString:nil];
[scanner scanString:@"<div class=\"postarea\">" intoString:nil];



[scanner scanUpToString:@"<div style=\"clear:both;\">" intoString:&extractedString];


if (extractedString)
{
// string was extracted
NSLog(@"%@", extractedString);
}
NSLog(@"BEFORE= %@",htmlString);
[htmlString appendString:extractedString];
NSLog(@"AFTER= %@",htmlString);
[htmlString appendString:@"</body></html>"];
NSLog(@"FINAL=%@",htmlString);

// check to see if file already exists in documents directory
if([self.fileMgr fileExistsAtPath:self.file])
{
NSLog(@"File already exists");
}
else
{
NSLog(@"file does not exist");
NSLog(@"Try creating the file");

// file does not already exist so create it
[self.fileMgr createFileAtPath:file contents:nil attributes:nil];

NSLog(@"%@",htmlString);
[htmlString writeToFile:self.file atomically:NO encoding:NSStringEncodingConversionAllowLossy error:nil];


//Test again whether this file exists now that we have tried creating it
if([self.fileMgr fileExistsAtPath:self.file])
{
NSLog(@"File exists now");
}
else
{
NSLog(@"File still doesn't exist");
}

}
}

在这种方法中,我只能从html格式的 extractString 的URL中选择部分并将其附加到htmlstring。当我尝试附加其他字符串时,它可以正常工作。

最佳答案

这是因为您传递了NSStringEncodingConversionAllowLossy作为encoding参数。您应该传递字符串编码参数之一。发生的是NSStringEncodingConversionAllowLossy等于1。但是writeToFile:atomically:encoding:error:期望使用字符串编码常量。等于1的字符串编码常量是NSASCIIStringEncoding。但是您的字符串无法转换为ASCII,因此您得到一个空文件。更改该行以使用UTF8,您将获得预期的非空文件:

[htmlString writeToFile:self.file atomically:NO encoding:NSUTF8StringEncoding error:nil];

您至少在某种程度上遇到了麻烦,因为 您没有检查返回类型或错误条件。该方法调用返回 BOOL,如果出现任何问题,将创建一个 NSError。如果您正在聆听,这将有助于您找出问题所在。如果您这样做:
NSError *saveError = nil;
if (![htmlString writeToFile:file atomically:NO encoding:NSStringEncodingConversionAllowLossy error:&saveError]) {
NSLog(@"Error writing file: %@", saveError);
}

您将看到一条错误消息,内容如下:
2012-11-21 11:20:47.250 Untitled[5731:707] Error writing file: Error Domain=NSCocoaErrorDomain Code=517 "The file “junk.html” couldn’t be saved using text encoding Western (ASCII)." UserInfo=0x7ffc0860b7c0 {NSFilePath=/tmp/junk.html, NSStringEncoding=1

乍一看可能并不明显,但是它特别提到尝试使用ASCII的事实可能是问题根源的重要线索。

关于iphone - HTML文件创建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13494905/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com