NSCFDictionary - 发送到实例的无法识别的选择器



我有两种模式 - 模式 1 和模式 2,可以通过中央按钮切换。在模式之间切换允许用户查看两种不同类型的群集注释。

可以轻松地从模式 1 切换到模式 2,但是当我切换回模式 1 时,我遇到了这个令人讨厌的错误

-[__NSCFDictionary componentsSeparatedByString:]: unrecognized selector sent to instance 0x7fb1308cca50

我正在打开我的代码并在TBClusteredAnnotations.m(我用于集群化的脚本)中打开我的代码。我有以下与componentsSeparatedByString相关的代码片段:

 TBQuadTreeNodeData TBDataFromLine(NSString *line)
{
    NSString *separator=@">>>>>>>>";
    NSArray *components = [line componentsSeparatedByString: separator];
    double latitude = [components[0] doubleValue];
    double longitude = [components[1] doubleValue];
    TBUserInfo* userInfo = malloc(sizeof(TBUserInfo));
    NSString *userName = [components[2] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    userInfo->userId = malloc(sizeof(char) * userName.length + 1);
    strncpy(userInfo->userId, [userName UTF8String], userName.length + 1);
    NSString *userId = [components [3] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    userInfo->userImage = malloc(sizeof(char) * userId.length + 1);
    strncpy(userInfo->userImage, [userId UTF8String], userId.length + 1);
    return TBQuadTreeNodeDataMake(latitude, longitude, userInfo);
}

但我正在MainViewController中使用以下代码加载我的注释:

-(void)annotationsmode1:(NSMutableArray*)marr
{
    if(marr.count>0)
    {
        NSMutableArray *newMarr1=[NSMutableArray new];
        NSString *separatestuff=@">>>>>>>>";
        for(NSMutableDictionary *dic in marr)
        {
            NSString *newStr=[NSString stringWithFormat:@"%@%@",[dic[@"latitude"] isEqualToString:@""]?@" ":dic[@"latitude"],separatestuff];
            newStr=[NSString stringWithFormat:@"%@%@%@",newStr,[dic[@"longitude"] isEqualToString:@""]?@" ":dic[@"longitude"],separatestuff];
            newStr=[NSString stringWithFormat:@"%@%@%@",newStr,[dic[@"id"] isEqualToString:@""]?@" ":dic[@"id"],separatestuff];
            newStr=[NSString stringWithFormat:@"%@%@",newStr,[dic[@"image"] isEqualToString:@""]?@" ":dic[@"image"]];
            [newMarr1 addObject:newStr];
        }
        //NSLog(@"NEW Array: %@",newMarr);
        [self.coordinateQuadTree buildTree:newMarr1];
    }
}
-(void)annotationsmode2:(NSMutableArray*)marr
{
    if(marr.count>0)
    {
        NSMutableArray *newMarr=[NSMutableArray new];
        NSString *separatestuff2=@">>>>>>>>";
        for(NSMutableDictionary *dic in marr)
        {
            NSString *newStr=[NSString stringWithFormat:@"%@%@",[dic[@"lat"] isEqualToString:@""]?@" ":dic[@"lat"],separatestuff2];
            newStr=[NSString stringWithFormat:@"%@%@%@",newStr,[dic[@"lang"] isEqualToString:@""]?@" ":dic[@"lang"],separatestuff2];
            newStr=[NSString stringWithFormat:@"%@%@%@",newStr,[dic[@"id"] isEqualToString:@""]?@" ":dic[@"id"],separatestuff2];
            newStr=[NSString stringWithFormat:@"%@%@",newStr,[dic[@"image"] isEqualToString:@""]?@" ":dic[@"image"]];
            [newMarr addObject:newStr];
        }
        //NSLog(@"NEW Array: %@",newMarr);
        [self.coordinateQuadTree buildTree:newMarr];
    }
}

更新:这是使用TBDataFromLine的代码块

- (void)buildTree:(NSMutableArray *)lines
{
    @autoreleasepool {
        NSInteger count = lines.count - 1;
        TBQuadTreeNodeData *dataArray = malloc(sizeof(TBQuadTreeNodeData) * count);
        for (NSInteger i = 0; i < count; i++) {
            dataArray[i] = TBDataFromLine(lines[i]);
        }
        //TBBoundingBox world = TBBoundingBoxMake(19, -166, 72, -53);
        TBBoundingBox world =  TBBoundingBoxMake(0,0,100,100);
        _root = TBQuadTreeBuildWithData(dataArray, count, world, 4);
    }
}

我已经在这个问题上工作了几个小时,但仍然毫无头绪。

更新:这是我得到的调试器的日志

    2015-06-20 19:40:23.759 MapProject[13426:395344] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary componentsSeparatedByString:]: unrecognized selector sent to instance 0x7fdf93775680'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000112699c65 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x00000001121c3bb7 objc_exception_throw + 45
    2   CoreFoundation                      0x00000001126a10ad -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x00000001125f713c ___forwarding___ + 988
    4   CoreFoundation                      0x00000001125f6cd8 _CF_forwarding_prep_0 + 120
    5   MapProject 0x000000010f469ac4 TBDataFromLine + 84
    6   MapProject 0x000000010f46a36f -[TBCoordinateQuadTree buildTree:] + 191
    7   MapProject 0x000000010f3d2503 -[MMViewController findSpot:] + 771
    8   UIKit                               0x0000000110a52da2 -[UIApplication sendAction:to:from:forEvent:] + 75
    9   UIKit                               0x0000000110b6454a -[UIControl _sendActionsForEvents:withEvent:] + 467
    10  UIKit                               0x0000000110b63919 -[UIControl touchesEnded:withEvent:] + 522
    11  UIKit                               0x0000000110a9f998 -[UIWindow _sendTouchesForEvent:] + 735
    12  UIKit                               0x0000000110aa02c2 -[UIWindow sendEvent:] + 682
    13  UIKit                               0x0000000110a66581 -[UIApplication sendEvent:] + 246
    14  UIKit                               0x0000000110a73d1c _UIApplicationHandleEventFromQueueEvent + 18265
    15  UIKit                               0x0000000110a4e5dc _UIApplicationHandleEventQueue + 2066
    16  CoreFoundation                      0x00000001125cd431 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    17  CoreFoundation                      0x00000001125c32fd __CFRunLoopDoSources0 + 269
    18  CoreFoundation                      0x00000001125c2934 __CFRunLoopRun + 868
    19  CoreFoundation                      0x00000001125c2366 CFRunLoopRunSpecific + 470
    20  GraphicsServices                    0x000000011444da3e GSEventRunModal + 161
    21  UIKit                               0x0000000110a51900 UIApplicationMain + 1282
    22  MapProject 0x000000010f474c2f main + 111
    23  libdyld.dy
)
libc++abi.dylib: terminating with uncaught exception of type NSException

添加一个异常断点,它将精确到发生崩溃的那一行。还要检查"行",因为它已更改为字典,但您的代码期望它 成为字符串

NSArray 或 NSMutableArray 上没有 [] 运算符。

你需要这个:

for (NSInteger i = 0; i < count; i++) {
    dataArray[i] = TBDataFromLine([lines objectAtIndex:i]);
}

在 NSArray* 上使用 [] 实际上就像"行"是指向 NSMutableArray 实例数组的指针,而您希望从它指向的数组中获取其中一个项目。

最新更新