iosobjective-cjsonitunesitunesartwork

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


I'm going to get music artwork from iTunes lookup API.

But on the way,
I had a problem with Xcode crashing...

Code(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];

※ if I insert "http://〜.jpg" directly instead of valuable artworkUrlString,it works.

Error log

    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

Solution

  • The thing is your artworkUrlString is not a NSString, but an NSArray.

    You can do this to get the information you need:

    // Convert to NSString Type
    
    NSArray *results = [parseDictionary objectForKey:@"results"];
    NSString *artworkUrlString = [[results firstObject] objectForKey:@"artworkUrl100"];
    
    NSLog(@"%@",artworkUrlString);