很好地使用NSXMLParser,将数组内的值全部设置为最后一个条目 [英] well using NSXMLParser the values inside an array are all set to the last entry

查看:60
本文介绍了很好地使用NSXMLParser,将数组内的值全部设置为最后一个条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用NSXMLParser解析YouTube的API,我得到了我想要的所有内容,并将其放入名为Video的类中.当我从 parserDidEndElement 内部编写类并结束该视频时,我将该类编写为 NSMutableArray .然后,我NSLog登录到阵列的视频的标题.每个视频都不同.我使用 addObject:方法将视频添加到阵列中.但是,当我使用 parserDidEndDocument 时,我使用了for循环来读取数组,并且所有条目都具有与最后添加的对象相同的标题值!这是怎么回事?

我在didEndElement中称呼它

Video *tempVideo = [Video new];
NSString *title = @"";
tempVideo = [allVideos objectAtIndex:[allVideos count]-1];
title = tempVideo.videoTitle;
NSLog(@"1videoTitle = %@", title);

返回每个元素 1videoTitle =(视频标题)

我在didEndDocument中称呼它为

int i;
Video *tempVideo = [Video new];
NSString *title = @"";
for(i=0;i<[allVideos count];i++){
    tempVideo = [allVideos objectAtIndex:i];
    title = tempVideo.videoTitle;
    NSLog(@"videoTitle = %@", title);
}

它返回每个元素 videoTitle =(所有19个视频的最后添加的视频标题)

解决方案

tc的答案是正确的,但让我重新措辞.

您错过了Objective-C的一个非常基本的细节.

Video *tempVideo = [Video new];
tempVideo = [allVideos objectAtIndex:[allVideos count]-1];

第一行声明一个变量tempVideo,它是指向Video类实例的指针,并为其分配对Video类的新分配实例的引用. /p>

第二行将tempVideo重新分配为对allVideos数组中对象的引用.它不是对象的副本,而是对数组中同一对象的引用.第一个实例-[Video new]中的一个实例已有效泄漏.

未显示的是在何处以及如何将Video对象添加到阵列中.

I use an NSXMLParser to parse YouTube's API and I get all the content I want and put it into a class called Video. When I am writing to the class from inside parserDidEndElement and I end that video I write the class to an NSMutableArray. Then I NSLog the title of the video that was logged into the array. This is different for each video. I use the addObject: method to add videos to the array. However when I use parserDidEndDocument I use a for loop to read through the array and all of the entries have the same title value of the last object added! What is going on here?

I call this in didEndElement:

Video *tempVideo = [Video new];
NSString *title = @"";
tempVideo = [allVideos objectAtIndex:[allVideos count]-1];
title = tempVideo.videoTitle;
NSLog(@"1videoTitle = %@", title);

It returns for each element 1videoTitle = (the videos title)

I call this in didEndDocument:

int i;
Video *tempVideo = [Video new];
NSString *title = @"";
for(i=0;i<[allVideos count];i++){
    tempVideo = [allVideos objectAtIndex:i];
    title = tempVideo.videoTitle;
    NSLog(@"videoTitle = %@", title);
}

It returns for each element videoTitle = (the last added videos title for all 19 of the videos)

解决方案

tc's answer is correct, but let me re-phrase.

You are missing a very basic detail of Objective-C.

Video *tempVideo = [Video new];
tempVideo = [allVideos objectAtIndex:[allVideos count]-1];

The first line declares a variable tempVideo that is a pointer to an instance of the Video class and assigns to it a reference to a freshly allocated instance of Video class.

The second line re-assigns tempVideo to be a reference to an object from the allVideos array. It is not a copy of the object, but a reference to the same object in the array. The first instance -- the one from [Video new] -- is effectively leaked.

What isn't shown is where and how you add the Video objects to the array.

这篇关于很好地使用NSXMLParser,将数组内的值全部设置为最后一个条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆