使用 NSXMLParser 检索到的数据填充 NSMutableArray 时出现问题

发布于 2024-12-25 07:54:57 字数 2309 浏览 3 评论 0原文

我正在开发我的第一个严肃的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寒冷纷飞旳雪 2025-01-01 07:54:57

在这一行中:

_stories = [_stories initWithCapacity:10];

您可能的意思是:

_stories = [[NSMutableArray alloc] initWithCapacity:10];

nil 数组上,所有 addObject: 消息都将被忽略。

结束元素后,您永远不会 nil'ing _currentTag

将此代码添加到 didEndElement: 的末尾

[_currentTag release];
_currentTag = nil;

,它应该按您的预期工作。

On this line:

_stories = [_stories initWithCapacity:10];

you probably meant:

_stories = [[NSMutableArray alloc] initWithCapacity:10];

On a nil array all the addObject: messages are ignored.

You're never nil'ing _currentTag after you end an element.

Add this code to the end of your didEndElement:

[_currentTag release];
_currentTag = nil;

and it should work as you expected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文