开发者

How to replace variables in an html file which is feeded to an UIWebView?

In my app, I want to replace some variables inside an html file when it goes to an UIWebView. The file is local. 开发者_如何学PythonFor example, I add a variable $userName$ and then I would replace that with @"John".

Would I have to read the file in as string, and then perform some string replacement action?


Yes, that should work... take a look at the replaceOccurrencesOfString:withString:options:range: method of NSMutableString. If there is much content and the position of your variable is not known exactly but is to be expected within known limits (for example within the first 100 characters), you might want to provide a range to reduce the overhead from comparing the whole content to the term you're about to replace. If the position of the variable is fixed, you could use replaceCharactersInRange:withString: instead. This would eliminate the performance penalty of comparing every part of the string to the string you're about to replace completely.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜