使用 NSXMLParser 检索到的数据填充 NSMutableArray 时出现问题
我正在开发我的第一个严肃的 iOS 应用程序,而且我对 Xcode、iOS 和 Objective-C 还很陌生。我在 iTunesU 上跟随并完成了斯坦福大学 iOS 课程的前几项作业。除此之外我没有任何经验。
无论如何,我使用 NSXMLParser 来解析从互联网检索到的 XML 文件,然后尝试创建 UGStory 对象来表示一些数据并将它们添加到 NSMutableArray 中。
编辑:我对代码做了一些更改并在此处更新。现在我确信我的 NSXMLParser 函数有问题。现在,当程序执行时,数组中最终有 30 个对象,但是当我检查数组时,每个对象都显示“超出范围”和“摘要不可用”。
此外,该数据还用于填充分割视图控制器。标题位于主视图中,链接用于填充详细视图。但执行后,master 中只有一件事; “News @ Ultimate-Guitar.Com”
这是标题中包含的第一个文本,此时尚未遇到标题。因此,我不应该有 UGStory 对象,因此我对如何设置此单元格感到困惑。此外,数组中的所有内容都“超出范围”,那么控制器从哪里获取这些数据呢?控制器只是创建一个 UGFeedReader 并向其发送 parseFeed 消息。
#import "UGFeedReader.h"
@implementation UGFeedReader
@synthesize currentTag = _currentTag;
@synthesize stories = _stories;
@synthesize read = _read;
@synthesize i = _i;
- (void)parseFeed
{
NSURL* ugFeed = [NSURL URLWithString:@"http://www.ultimate-guitar.com/modules/rss/news.xml.php"];
NSXMLParser* feedParser = [[NSXMLParser alloc]initWithContentsOfURL:ugFeed];
_stories = [[NSMutableArray alloc] initWithCapacity:10];
_i = 0;
_read = NO;
[feedParser setDelegate:self];
[feedParser parse];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict {
if ([elementName isEqualToString:@"item"])
{
_read = YES;
UGStory* temp = [[UGStory alloc] init];
[_stories addObject:temp];
}
return;
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if (!_currentTag)
{
_currentTag = [[NSMutableString alloc] init];
}
[_currentTag appendString:string];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if (_read == YES)
{
if ([elementName isEqualToString:@"title"]) {
UGStory* temp = [_stories objectAtIndex:_i];
temp.title = _currentTag;
}
if ([elementName isEqualToString:@"link"]) {
UGStory* temp = [_stories objectAtIndex:_i];
temp.url = [NSURL URLWithString:_currentTag];
_read = NO;
_i++;
}
}
return;
}
@end
I'm working on my first somewhat serious iOS app and I'm new to Xcode, iOS, and Objective-C. I've followed along with, and done a the first few assignments from Stanford's iOS class on iTunesU. Aside from that I have no experience.
Anyways, I'm using an NSXMLParser to parse an XML file retrieved from the internet, and then I'm trying to create UGStory objects to represent some of the data and add them to an NSMutableArray.
EDIT: I've made some changes to the code and updated it here. Now I am sure there is a problem with my NSXMLParser functions. Now when the program executes I end up with 30 objects in the array, but when I inspect the array every object says "Out of Scope" and "Summary Unavailable".
Also, this data is being used to populate a split view controller. Titles go in the masterview, links are used to fill the detailview. But after this executes there is only one thing in the master; "News @ Ultimate-Guitar.Com"
That is the first text enclosed by headers and at this point the header is yet to be encountered. Because of this I should have no UGStory objects so I'm confused by how this cell is being set. Also everything in the array is "Out of Scope" so where is the controller getting this data from? The controller does nothing more than create a UGFeedReader and send it the parseFeed message.
#import "UGFeedReader.h"
@implementation UGFeedReader
@synthesize currentTag = _currentTag;
@synthesize stories = _stories;
@synthesize read = _read;
@synthesize i = _i;
- (void)parseFeed
{
NSURL* ugFeed = [NSURL URLWithString:@"http://www.ultimate-guitar.com/modules/rss/news.xml.php"];
NSXMLParser* feedParser = [[NSXMLParser alloc]initWithContentsOfURL:ugFeed];
_stories = [[NSMutableArray alloc] initWithCapacity:10];
_i = 0;
_read = NO;
[feedParser setDelegate:self];
[feedParser parse];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict {
if ([elementName isEqualToString:@"item"])
{
_read = YES;
UGStory* temp = [[UGStory alloc] init];
[_stories addObject:temp];
}
return;
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if (!_currentTag)
{
_currentTag = [[NSMutableString alloc] init];
}
[_currentTag appendString:string];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if (_read == YES)
{
if ([elementName isEqualToString:@"title"]) {
UGStory* temp = [_stories objectAtIndex:_i];
temp.title = _currentTag;
}
if ([elementName isEqualToString:@"link"]) {
UGStory* temp = [_stories objectAtIndex:_i];
temp.url = [NSURL URLWithString:_currentTag];
_read = NO;
_i++;
}
}
return;
}
@end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这一行中:
您可能的意思是:
在
nil
数组上,所有addObject:
消息都将被忽略。结束元素后,您永远不会
nil
'ing_currentTag
。将此代码添加到
didEndElement:
的末尾,它应该按您的预期工作。
On this line:
you probably meant:
On a
nil
array all theaddObject:
messages are ignored.You're never
nil
'ing_currentTag
after you end an element.Add this code to the end of your
didEndElement:
and it should work as you expected.