【问题标题】:How do you extract part of an NSUserDefault to put into a UITableView, UIButton text or even a UILabel?如何提取 NSUserDefault 的一部分以放入 UITableView、UIButton 文本甚至 UILabel?
【发布时间】:2012-04-15 08:32:46
【问题描述】:

我已经使用以下方法缓存了 url 的内容,并将文本保存为 NSUserDefault,因为我需要在另一个 UIViewController 中访问它。

这是我用来下载 URL 内容并保存为 NSUserDefault 的代码,这是我用来将该 NSUserDefault 的内容加载到 UITableView 中的代码。

//下载URL内容并保存到NSUser Default

NSString *url = [[NSString stringWithFormat:@"http://www.pharmacon.site50.net/database/1.xml"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *myTxtFile = [NSString stringWithContentsOfURL:[NSURL URLWithString:url] encoding:NSUTF8StringEncoding error:&err];
[[NSUserDefaults standardUserDefaults] setObject:myTxtFile forKey:@"one"];

//加载一个NSUserDefault并将其放置在一个UITextView中

NSString *myTxtFile = [[NSUserDefaults standardUserDefaults] stringForKey:@"one"];
textView.text=myTxtFile; 

这是我下载的典型 XML 文件的样子;

<item>
<din>1</din>
<category>Drugs</category>
<name>Paracetamol</name>
<phonetic>Pa-ra-ce-ta-mol</phonetic>
<spoken>Paracetamol.mp3</spoken>
<description>
Paracetamol is a drug commonly used for headaches and minor aches and pains.
</description>
<purpose>
Paracetamol is best used for minor to moderate aches and pains and can also be used to treat mild headaches.
</purpose>
<author>Sam Vale</author>
<editor>Harrison Fuller</editor>
</item>

现在,我想做的是阅读这个 NSUserDefault 但实际上只提取名称区域中的文本(在本例中为“扑热息痛”),然后将其放入 UITableView 单元格的文本中,作为按钮或 UILabel 的文本(选择您认为最好的或您知道的)。我这样做的原因是因为我要在 UITableView 中显示从数据库下载的结果。

任何帮助将不胜感激。

【问题讨论】:

    标签: iphone xml xcode nsstring xml-parsing


    【解决方案1】:

    为此,您需要NSXMLParser。网上有很多关于如何使用它来解析 XML 的例子。

    【讨论】:

    • 感谢您的帮助。我会投票赞成您的回复,但我的声誉只有 10 分,因为我是 Stack Overflow 的新手,您需要 15 分才能获得声誉才能投票。
    猜你喜欢
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多