gpt4 book ai didi

iphone - NSString(方法调用 : stringWithContentsOfFile)

转载 作者:行者123 更新时间:2023-12-03 16:47:05 26 4
gpt4 key购买 nike

NSBundle *bundle = [NSBundle bundleForClass : [self class]];
NSString *f_path = nil;

if ((f_path = [bundle pathForResource : @"about_screen" ofType:@"html" inDirectory:@"html"]) != nil)
{
NSLog(@" f_path found" );
NSString *ns_string = [NSString stringWithContentsOfFile : f_path
encoding : NSUTF8StringEncoding
error : NULL
];
NSLog(@" string = %@", ns_string);
}
else
{
NSLog(@" f_path not found" );

}

// *** if the following assignment is commented off, there will be an error. ***
ns_string =
@"<!DOCTYPE html PUBLIC \"-//IETF//DTD HTML 2.0//EN\"><HTML><HEAD><TITLE>minimal test </TITLE></HEAD><BODY bgcolor = \"silver\"><H1>Hi</H1><P>This is very minimal \"hello world\" test document.</P> </BODY></HTML>";


[web_view loadHTMLString : ns_string baseURL : nil];

考虑上面的代码段。

出于测试目的,我将文件“about_screen.html”的内容设置为与上面代码中分配给 ns_string 的字符串相同。因此,如果“NSString stringWithContentsOfFile”按预期工作,则可以注释掉 ns_string“内联”赋值,而不会产生任何影响。

我的问题是:内联赋值按预期工作,但如果没有它,将会出现运行时错误。

错误是:

-[About_Screen dataUsingEncoding:]: unrecognized selector sent to instance 0x6eddf70'

另请注意该声明:

NSLog(@" string = %@", ns_string);

始终输出正确的字符串,因此可以找到并正确读取嵌入的 html 文件。

希望有懂这方面知识的 friend 帮忙解答一下。

最佳答案

这个答案实际上来自@bbamhart的评论。我认为问题是由于 NSUTF8StringEncoding 参数造成的,因为它经常在谷歌搜索相关主题时出现。

问题的真正原因是变量作用域。 ns_string 不应在任何条件 block 中声明。 (因为该变量通常无法在 block 之外访问。)

最根本的解决方案是将声明移出 block 。

关于iphone - NSString(方法调用 : stringWithContentsOfFile),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12020092/

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