2
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:]: インスタンス 0x6eddf70 に送信された認識されないセレクター

また、次のステートメントにも注意してください。

NSLog(@" 文字列 = %@", ns_string);

は常に正しい文字列を出力するため、埋め込まれた html ファイルが検出され、正しく読み取られます。

これに詳しい人が助けてくれることを願っています。

4

1 に答える 1

0

This answer actually comes from @bbamhart's comment. I thought the problem was due to the NSUTF8StringEncoding parameter since it shows up frequently from googling on related topics.

The real cause of the problem is about variable scope. ns_string should not be declared in any conditional blocks. (since the variable would then normally not be accessible outside the block.)

The bottom-line solution is simply move the declaration out of the block.

于 2012-08-18T19:04:39.713 に答える