0
我真的絕望在這一個。我試圖製作一個框架,您可以搜索和播放YouTube視頻。但是在測試時,我遇到了一個很大的問題。問題與NSMutableArray和自定義NSObject
在搜索操作中,我將YTVideos(NSObject的一個子類)添加到NSMutableArray中。當我環通它在main(),我得到零對象:
方法
- (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;
}
的main()
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類別)。
也許只是標題爲零?我認爲'YTVideo'中的'initWithXMLElement'方法可能存在問題。我們可以看看YTVideo課嗎? – 2010-08-13 17:41:24
感謝您的回覆,我更新了我的問題 – v1Axvw 2010-08-13 22:34:49
您從這個程序的輸出中看到了什麼?它是否打印出「nil」或「」(null)「'? – 2010-08-14 08:35:17