【问题标题】:Youtube ios feed thumbnailYoutube ios 提要缩略图
【发布时间】:2013-03-30 20:38:25
【问题描述】:

我正在创建一个应用来获取我朋友的 youtube 供稿。当我运行应用程序时,我收到一个错误:

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 收到它。请帮助

【问题讨论】:

    标签: iphone ios objective-c youtube-api


    【解决方案1】:

    您收到错误是因为您的字典正在为该键返回一个 nil 对象...因此,您首先应确保您的 video 字典为您的 @"data.items.thumbnail.hqdefault" 键返回一个非 nil 对象(字符串),然后才初始化你的 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
    }
    

    【讨论】:

    • 为什么它返回一个 nil 对象?
    • 您的密钥不正确或从未为该密钥设置字符串。
    • 你不能,我再说一遍不能将key paths 与-objectForKey: 一起使用。它根本行不通。如果您必须使用密钥路径而不是多个-objectForKey: 调用,请改用-valueForKeyPath:
    • 我使用了这个developers.google.com/youtube/2.0/…,缩略图是路径的正确对象
    • 我更新以反映 valueForKeyPath:... 哎呀。因此,也许该字符串从未设置,并且您的视频对象不完整。不过,保障措施仍然是一个好主意。我在任何地方都使用它们...还有NSDictionary 类参考:developer.apple.com/library/mac/#documentation/Cocoa/Reference/…
    猜你喜欢
    • 2012-12-02
    • 2012-12-15
    • 2014-04-07
    • 1970-01-01
    • 2016-04-18
    • 2014-08-05
    • 2017-04-25
    • 2015-03-26
    • 2012-02-21
    相关资源
    最近更新 更多