我真的非常绝望。我正在尝试制作一个框架,您可以搜索和播放YouTube视频。但在测试时,我遇到了一个大问题。
在搜索操作中,我将YTVideos(NSObject的子类)添加到NSMutableArray。当我在main()中循环它时,我得到的是nil-objects:
方式
- (NSArray *)videosInRange:(NSRange)range {
if(range.length > 50) {
[NSException raise:@"Range lenth > 50"
format:@"The range of -videosInRange: can't be bigger than 50"];
return nil;
}
if((range.location + range.length) > 999) {
[NSException raise:@"Range to big"
format:@"The given range was to big (%d, %d)", range.location, range.length];
return nil;
}
NSString *searchURLString = [[self feedURL] absoluteString];
searchURLString = [searchURLString stringBySettingURLAttribute:@"start-index" value:[NSString stringWithFormat:@"%d",range.location + 1]];
searchURLString = [searchURLString stringBySettingURLAttribute:@"max-results" value:[NSString stringWithFormat:@"%d",range.length]];
NSLog(@"%@",searchURLString);
NSURL *url = [NSURL URLWithString:searchURLString];
NSXMLDocument *xmlDoc = [[NSXMLDocument alloc] initWithContentsOfURL:url
options:0
error:NULL];
if(!xmlDoc)
return nil;
NSArray *videoElements = [[xmlDoc rootElement] elementsForName:@"entry"];
NSMutableArray *videos = [[NSMutableArray alloc] initWithCapacity:[videoElements count]];
register int i;
for(i = 0; i < [videoElements count]; i++) {
NSAutoreleasePool *addPool = [[NSAutoreleasePool alloc] init];
YTVideo *vid = [[YTVideo alloc] initWithXMLElement:[videoElements objectAtIndex:i]];
[videos addObject:vid];
[vid release];
[addPool drain];
}
NSArray *retValue = [NSArray arrayWithArray:videos];
[videos release];
return retValue;
}
主要()
int main(int argc, const char *argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
YTSearchFeed *feed = [[YTSearchFeed alloc] initWithSearch:@"Eminem"];
long long results = [feed videoCount];
NSLog(@"%lld videos for search", results);
long long i = 0;
while(results != 0) {
int length = (results >= 50) ? (50) : (results);
NSArray *videos = [feed videosInRange:NSMakeRange(i, length)];
NSLog(@"L: %d", [videos count]);
int z;
for(z = 0; z < [videos count]; z++, i++) {
YTVideo *vid = [videos objectAtIndex:z];
NSString *title = [vid title];
NSLog(@"%d: %@", i+1, title);
}
results -= length;
}
[pool drain];
return NSApplicationMain(argc, argv);
}
我希望有人可以花时间看看这个,如果你需要更多信息,请问。
提前谢谢你, ief2
编辑:YTVideo
- (id)initWithXMLElement:(NSXMLElement *)element {
self = [super init];
if(self != nil) {
_XMLElement = [element copy];
}
return self;
}
- (NSString *)title {
if(!_title) {
NSString *str = [[[self XMLElement] firstElementWithName:@"title"] stringValue];
_title = [[str stringByDecodingHTMLEntities] retain];
}
return [[_title copy] autorelease];
}
只有在请求时才能获得标题(以及其他视频信息)。 -stringByDecodingHTMLEntities工作正常(NSString上的类别)。
答案 0 :(得分:0)
我重写了代码并初始化了-init
方法