<Error/ Objective C> -[__NSArrayI length]: 无法识别的选择器发送到实例

<Error/ Objective C> -[__NSArrayI length]: unrecognized selector sent to instance

我要从 iTunes 查找中获取音乐作品 API。

但是在路上,
我遇到了 Xcode 崩溃的问题...

代码(Objective C)

/////////// Get artwork from iTunes API/////////////
NSString *urlString = @"http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/wa/wsLookup?id=954758417&country=US";
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
    if(!connectionError){
        NSError* parseError;
        NSDictionary *parseDictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&parseError];


        ///// Get artwork /////

        // Convert to NSString Type
        NSString *artworkUrlString = [parseDictionary valueForKeyPath:@"results.artworkUrl100"];

        NSLog(@"%@",artworkUrlString); 
        //=>("http://is2.mzstatic.com/image/pf/us/r30/Music3/v4/59/91/cc/5991cc91-28d6-9f29-f7ce-a32614c4a388/093624930655.100x100-75.jpg")

        // X Code Crashing below. but I don't know how to solve it.
        NSURL *artworkUrl = [NSURL URLWithString:artworkUrlString];

※ 如果我直接插入 "http://〜.jpg" 而不是有价值的 artworkUrlString,它会起作用。

错误日志

    2015-03-30 00:26:19.891 Project[1385:180616] 
    -[__NSArrayI length]: unrecognized selector sent to instance 0x174039900

    2015-03-30 00:26:19.893 Project[1385:180616] ***                                        
    Terminating app due to uncaught exception 'NSInvalidArgumentException',    
    reason: '-[__NSArrayI length]: unrecognized selector sent to instance  
    0x174039900'

    *** First throw call stack:
    (0x1859a2530 0x1969780e4 0x1859a95f4 0x1859a63ac 0x1858aac4c 0x18597b0e8         
    0x1867cc104 0x1867cbf6c 0x1000177dc 0x185386cf0 0x18687fbe8 0x1867d1374 
    0x1867c0ecc 0x18688294c 0x1000b8f94 0x1000bdc28 0x18595a2ec 0x185958394 
    0x1858851f4 0x18eca76fc 0x18a21610c 0x10001f670 0x196ff6a08)
    libc++abi.dylib: terminating with uncaught exception of type NSException

看起来 artworkUrlString 不是 NSString,而是 NSArray 对象。在控制台中确认这个简单的 运行 [artworkUrlString class]

问题是你的 artworkUrlString 不是 NSString,而是 NSArray。

您可以这样做来获取您需要的信息:

// Convert to NSString Type

NSArray *results = [parseDictionary objectForKey:@"results"];
NSString *artworkUrlString = [[results firstObject] objectForKey:@"artworkUrl100"];

NSLog(@"%@",artworkUrlString);