NSCFArray length]:错误,数组正则表达式

时间:2010-09-29 19:17:36

标签: iphone objective-c regex itunes itunesconnect

  StringReply = [[NSString alloc] initWithData:dataReply encoding:NSUTF8StringEncoding];

         //Regex Out Artist Name
         //NSString *regEx = ; 
         NSArray *iTunesAristName = [stringReply componentsMatchedByRegex: @"(?<=artistname\":\")([^<]+)(?=\")"]; 

         if ([iTunesAristName isEqual:@""]) { 
           NSLog(@"Something has messed up");
           //Regex Out Song Name
          }else{
           NSLog(iTunesAristName);
          }

         NSLog(iTunesAristName);
         [stringReply release];

我一直收到这个错误?

        2010-09-29 21:15:16.406 [2073:207] *** -[NSCFArray length]: unrecognized selector sent to instance 0x4b0b800
        2010-09-29 21:15:16.406 [2073:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFArray length]: unrecognized selector sent to instance 0x4b0b800'
        2010-09-29 21:15:16.407 [2073:207] Stack: (
请帮助它让我疯狂

3 个答案:

答案 0 :(得分:0)

NSLog的第一个参数应该是格式字符串。你正在通过NSArray。当函数尝试将数组视为字符串时,您会收到该错误。相反,请使用NSLog(@"%@", iTunesAristName);

答案 1 :(得分:0)

Chuck回答了你的问题,但我注意到其他一些问题。

NSArray是一个数组,而不是字符串,因此[iTunesArtistName isEqual:@""]永远不会返回true,因为它们是不同的类。即使iTunesArtistName是字符串,也应使用isEqualToString:方法进行比较,而不是isEqual:

如果您只想提取艺术家的名字,您可以这样做:

NSArray *matches = [stringReply componentsMatchedByRegex: @"(?<=artistname\":\")([^<]+)(?=\")"]; 

if ([matches count] == 0)
{
    NSLog(@"Could not extract the artist name");
}
else
{
    NSString *iTunesArtistName = [matches objectAtIndex:0];

    NSLog(@"Artist name: %@", iTunesArtistName);
}

答案 2 :(得分:0)

我看到你正在使用RegexKitLite,请确保你导入了libicucore.dylib,在导入该库之前我遇到了同样的错误。