NSCFArray长度]:误差,阵列的正则表达式
-
26-09-2019 - |
题
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: (
请帮助其驾驶我疯狂。
解决方案
要的NSLog的第一个参数被认为是一个格式字符串。你传递一个NSArray。当函数试图把你的数组作为字符串,你得到这个错误。取而代之的是,使用NSLog(@"%@", iTunesAristName);
。
其他提示
查克已经回答了你的问题,但我发现别的东西是有问题的。
的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);
}
我看你使用RegexKitLite,确保导入libicucore.dylib,我居然也得到了同样的错误,直到我导入的库。
不隶属于 StackOverflow