iphone中的NSDictionary解析异常

问题描述

| 我正在使用此代码,它给异常
    NSMutableArray *streams = (NSMutableArray *)[feed valueForKey:@\"comments\"];
    NSMutableArray *streams1 = (NSMutableArray *)[streams valueForKey:@\"data\"];
    //NSMutableArray *streams2 = (NSMutableArray *)[streams1 valueForKey:@\"message\"];
    // loop over all the stream objects and print their titles
    int index;
    NSMutableDictionary *stream;
  for (index = 0; index < [feed count];index++) {
        stream = (NSMutableDictionary *)[streams1 objectAtIndex:index];

                NSLog(@\"Name of sender is: %@\",[stream valueForKey:@\"message\"]); 

        }


FaceBookTable *detailViewController = [[FaceBookTable alloc] initWithNibName:@\"FaceBookTable\" bundle:nil];
    // ...
    // Pass the selected object to the new view controller.
    detailViewController.fbGraph = fbGraph;
    detailViewController.dummyArray  = [ feed valueForKey:@\"message\"];
    detailViewController.dict = stream;

}
例外是
-[__NSArrayI isEqualToString:]: unrecognized selector sent to instance 0x5dae960
2011-06-15 16:14:07.835 MultiSocial[8042:207] *** Terminating app due to uncaught exception \'NSInvalidArgumentException\',reason: \'-[__NSArrayI isEqualToString:]: unrecognized selector sent to instance 0x5dae960\'
当我的其他代码工作正常时
    NSMutableArray *streams = (NSMutableArray *)[feed valueForKey:@\"from\"];
    lueForKey:@\"message\"];
    // loop over all the stream objects and print their titles
    int index;
    NSMutableDictionary *stream;
for (index = 0; index < [feed count];index++) {
        stream = (NSMutableDictionary *)[streams objectAtIndex:index];

                NSLog(@\"Name of sender is: %@\",[stream valueForKey:@\"message\"]); 



    }
请帮忙 这是崩溃日志
2011-06-15 17:05:42.148 MultiSocial[8583:207] -[__NSArrayI isEqualToString:]: unrecognized selector sent to instance 0x5d05f50
2011-06-15 17:05:42.156 MultiSocial[8583:207] *** Terminating app due to uncaught exception \'NSInvalidArgumentException\',reason: \'-[__NSArrayI isEqualToString:]: unrecognized selector sent to instance 0x5d05f50\'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x013cabe9 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x0151f5c2 objc_exception_throw + 47
    2   CoreFoundation                      0x013cc6fb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
    3   CoreFoundation                      0x0133c366 ___forwarding___ + 966
    4   CoreFoundation                      0x0133bf22 _CF_forwarding_prep_0 + 50
    5   UIKit                               0x00649405 -[UITableViewLabel setText:] + 84
    6   MultiSocial                         0x00046147 -[FaceBookTable tableView:cellForRowAtIndexPath:] + 467
    7   UIKit                               0x0045f7fa -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:withIndexPath:] + 634
    8   UIKit                               0x0045577f -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:] + 75
    9   UIKit                               0x0046a450 -[UITableView(_UITableViewPrivate) _updateVisibleCellsNow:] + 1561
    10  UIKit                               0x00462538 -[UITableView layoutSubviews] + 242
    11  QuartzCore                          0x01e98451 -[CALayer layoutSublayers] + 181
    12  QuartzCore                          0x01e9817c CALayerLayoutIfNeeded + 220
    13  QuartzCore                          0x01e9137c _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 310
    14  QuartzCore                          0x01e910d0 _ZN2CA11Transaction6commitEv + 292
    15  QuartzCore                          0x01ec17d5 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 99
    16  CoreFoundation                      0x013abfbb __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 27
    17  CoreFoundation                      0x013410e7 __CFRunLoopDoObservers + 295
    18  CoreFoundation                      0x01309bd7 __CFRunLoopRun + 1575
    19  CoreFoundation                      0x01309240 CFRunLoopRunSpecific + 208
    20  CoreFoundation                      0x01309161 CFRunLoopRunInMode + 97
    21  GraphicsServices                    0x0190f268 GSEventRunModal + 217
    22  GraphicsServices                    0x0190f32d GSEventRun + 115
    23  UIKit                               0x003fa42e UIApplicationMain + 1160
    24  MultiSocial                         0x00002740 main + 102
    25  MultiSocial                         0x000026d1 start + 53
)
    

解决方法

        根据崩溃日志-[__ NSArrayI isEqualToString:],代码中有一些错误。在崩溃报告中可以清楚地看到isEqualToString方法用于NSString对象,但是在您的代码中它是在NSArray上调用的。 只需调试您的应用程序,您就会找到解决方案。 更新
//Make sure that feed return an array for the key comments
 NSArray *streams = (NSArray *)[feed valueForKey:@\"comments\"];
 NSArray *streams1 = (NSArray *)[streams valueForKey:@\"data\"];

 NSLog(@\"comments : %@\",streams);

//When you are running loop only last index value will be copied into your dictionary named stream so remove the loop.

FaceBookTable *detailViewController = [[FaceBookTable alloc] initWithNibName:@\"FaceBookTable\" bundle:nil];
    // ...
    // Pass the selected object to the new view controller.
    detailViewController.fbGraph = fbGraph;
    detailViewController.dummyArray  = [ feed valueForKey:@\"message\"];
    detailViewController.dict = streams;

}
//您的tableView cellForRowAtIndexPath方法应如下所示
//First get the dictionary

if(dict){
        NSDictionary *stream = (NSDictionary *)[dict objectAtIndex:indexPath.row];

       if(stream){
            NSDictionary * messages = (NSDictionary *[stream valueForKey:@\"Message\"];

            NSString *comment = [messages valueForKey:@\"comment\"];

           cell.textLabel.text = comment;
       }
}
    ,        你已经将NSString对象分配给NSArray.so做断点调试     ,        
NSMutableArray *arr = [[[feed valueForKey:@\"comments\"]valueForKey:@\"data\" ]valueForKey:@\"id\"];
for (NSString* cid in [arr objectAtIndex:0]) {
    NSLog(@\"cid is : %@ \\n\\n\",cid);
}
    

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...