开发者

Initializing NSStrings with Large, constant html strings

I have a large constant html string which I'd like to display on my UIWebView. For some reason, when I try to use the constant string ( with @"LARGE_STRING" ) I get some weird errors, saying I have an unexcpected '@' at the start, that I miss terminating ' " ' characters, that I use undeclared expressions and etc. the thing is - I do escape the string, atleast the ' " ' in it.

What am I missing in here?

Tnx!

Edit: Example:

NSString *myString = [NSString stringWithString:@"<title>בסיסי HTML לימוד</title> <META http-equiv="Content-Type" content="text/html; charset=iso-8859-8"> <body  bgcolor="#b5deef"bbackground="gifs/rekablue.gif"> <center> <table border=0 cellpadding=0 cellspacing=0> <tr> <td> <table border=0 cellpadding=0 cellspacing=0> <tr> <td> <table border=0 cellpadding=0 cellspacing=0><tr> <td width="246"></td> <td><img src="gifs/lim.gif"></td><td width="50"></td><td><a href="basic1.htm"><img src="gifs/hak.gif" border=0></a></td> </tr></table> <tr><td> <table border=0 cellpadding=0 cellspacing=0><tr>  <td width="230"></td> <td><img src="gifs/m.gif"></td><td width="55"></td><td>&开发者_JAVA技巧amp;lt;a href="basic2.htm"><img src="gifs/ktiv.gif" border=0></a></td> </tr></table> <tr><td> <table border=0 cellpadding=0 cellspacing=0><tr> <td width="207"></td> <td><img src="gifs/ud.gif"></td><td width="60"></td><td><a href="basic2.htm"><img src="gifs/amud.gif" border=0></a></td> </tr></table> <tr><td> <table border=0 cellpadding=0 cellspacing=0><tr> <td width="163"></td> <td><img src="gifs/tl.gif"></td><td width="55"></td><td><a href="basic2.htm"><img src="gifs/reshon.gif" border=0></a></td> </tr></table> <tr><td> <table border=0 cellpadding=0 cellspacing=0><tr> <td width="133"></td> <td><img src="gifs/ml.gif"></td><td width="55"></td><td><a href="basic3.htm"><img src="gifs/text1.gif" border=0></a></td>  </tr></table> <tr><td> <table border=0 cellpadding=0 cellspacing=0><tr> <td width="108"></td> <td><img src="gifs/t.gif"></td><td width="35"></td><td><a href="basic4.htm"><img src="gifs/kish.gif" border=0></a></td> </tr></table> <tr><td> <table border=0 cellpadding=0 cellspacing=0><tr> <td width="93"></td> <td><img src="gifs/h.gif"></td><td width="35"></td><td><a href="basic5.htm"><img src="gifs/reshim1.gif" border=0></a></td> </tr></table> <tr><td> <table border=0 cellpadding=0 cellspacing=0><tr> <td width="70"></td> <td><img src="gifs/b.gif"></td><td width="42"></td><td><a href="basic6.htm"><img src="gifs/tmunot.gif" border=0></a></td> </tr></table> <tr><td>  <table border=0 cellpadding=0 cellspacing=0><tr> <td width="40"></td> <td><img src="gifs/s.gif"></td><td width="53"></td><td><a href="basic7.htm"><img src="gifs/rekaim1.gif" border=0></a></td> </tr></table> <tr><td> <table border=0 cellpadding=0 cellspacing=0><tr> <td width="10"></td> <td><img src="gifs/s1.gif"></td><td width="45"></td><td><a href="basic9.htm"><img src="gifs/tfasim.gif" border=0></a></td> </tr></table> <tr><td> <table border=0 cellpadding=0 cellspacing=0><tr> <td width="8"></td> <td valign=top><img src="gifs/si.gif"></td><td width="30"></td><td><a href="basic8.htm" border=0><img src="gifs/tavlaot.gif" border=0></a></td> </tr></table> </td></tr> </table> </td>  <td valign=bottom><a href="default.htm" onMouseOver="self.status='חזרה לעמוד פתיחה';return true" onMouseOut="self.status='';return true"><img src="gifs/shtml11.gif" border=0></a>   <a href="bm_guide.htm#html" onMouseOver="self.status='אתרים נוספים';return true" onMouseOut="self.status='';return true"><img src="gifs/shtml10.gif" border=0></a></td></tr></table> </center> </body>"];


If you have " characters in the string, then you must escape them:

    NSString *myString = @"This string contains \"quotes\". If they are not \"escaped\", that will cause errors".

Or, another possibility, you are using #defines like

#define LARGE_STRING "This is an extremely large string (well, OK, not so large)"

Then you should do

#define LARGE_STRING @"This is an extremely large string (well, OK, not so large)"

and

    NSString *myString = LARGE_STRING;

Edit

Hmmm... your example looks fine. It should compile, if it really contains the HTML-style " codes and not hard-coded " characters. Just make sure your string (it is really long indeed) is not wrapped with hard linefeeds.

I just tried, and your example compiles fine, in Objective-C (Xcode 4.1). Note that if you try to pass such a string to a C function, it should be converted to a valid C string first.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜