2013-03-30 98 views
0

我正在創建一個應用程序來拉我的朋友YouTube的飼料。當我運行應用程序,我收到一個錯誤:Youtube ios feed thumbnail

2013-03-30 16:34:04.973 eastcoastdyes[15395:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL initWithString:relativeToURL:]: nil string parameter' 
*** First throw call stack: 
(0x1a38012 0x1354e7e 0x1a37deb 0xd4a49d 0xdc11e4 0x3859 0x3538fb 0x3539cf 0x33c1bb 0x34cb4b 0x2e92dd 0x13686b0 0x3014fc0 0x300933c 0x3009150 0x2f870bc 0x2f88227 0x2f888e2 0x1a00afe 0x1a00a3d 0x19de7c2 0x19ddf44 0x19dde1b 0x19927e3 0x1992668 0x298ffc 0x231d 0x2245 0x1) 
libc++abi.dylib: terminate called throwing an exception 
(lldb) 

這裏是我的代碼:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *cellID = @"Cell Identifier"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID]; 
    if (!cell) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID]; 
    } 

    NSDictionary *video = [self.videos objectAtIndex:indexPath.row]; 
    cell.textLabel.text = [video objectForKey:@"title"]; 
    cell.detailTextLabel.text = [video objectForKey:@"uploader"]; 

    NSURL *url = [[NSURL alloc] initWithString:[video objectForKey:@"data.items.thumbnail.hqdefault"]]; 
    [cell.imageView setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeholder"]]; 

    return cell; 

}

我接受它在NSURL * URL ...幫助請

回答

1

您收到錯誤是因爲您的字典正在返回該鍵的零對象...因此,您應該首先確保video字典爲您的返回一個非零對象(字符串)鍵,然後才初始化您的URL ...

例如

NSString *imageURLString = [video valueForKeyPath:@"data.items.thumbnail.hqdefault"]; 
if (imageURLString) { 
    NSURL *url = [[NSURL alloc] initWithString:imageURLString]; 
    [cell.imageView setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeholder"]]; 
} else { 
// set imageView to something else 
} 
+0

爲什麼它返回一個零對象? –

+0

您的密鑰要麼不正確,要麼沒有爲該密鑰設置字符串。 – Alec

+0

你不能,我再說一遍,不能用'key paths'和-objectForKey :.它根本無法工作。如果您必須使用關鍵路徑而不是多個「-objectForKey:」調用,請改用「-valueForKeyPath:」。 –