iPhone 中的 NSDictionary 解析异常

发布于 2024-11-15 15:58:15 字数 4625 浏览 2 评论 0原文

我正在使用这段代码,它给出了异常

    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
)

I am using this code and it is giving exception

    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;

}

Exception is

-[__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'

while my other code is working fine

    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"]); 



    }

Please help

here is crash log

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
)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半山落雨半山空 2024-11-22 15:58:15

根据崩溃日志 -[__NSArrayI isEqualToString:] 您的代码中有一些错误。在崩溃报告中可以清楚地看到 isEqualToString 方法适用于 NSString 对象,但在您的代码中它是在 NSArray 上调用的。

只需调试您的应用程序,您就会找到解决方案。

UPDATE

//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;
       }
}

Well as per the crash log -[__NSArrayI isEqualToString:] there is some mistake in your code. It is clearly visible in the crash report that isEqualToString method is for NSString object but in your code it is called on NSArray.

Just debug your app and you will find the solution.

UPDATE

//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;

}

//Your tableView cellForRowAtIndexPath method should look like this

//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;
       }
}
遇到 2024-11-22 15:58:15

你已经将 NSString 对象分配给 NSArray.so 使用断点进行调试

u have been assigning the NSString object to NSArray.so do debug with breakpoints

孤独患者 2024-11-22 15:58:15
NSMutableArray *arr = [[[feed valueForKey:@"comments"]valueForKey:@"data" ]valueForKey:@"id"];
for (NSString* cid in [arr objectAtIndex:0]) {
    NSLog(@"cid is : %@ \n\n",cid);
}
NSMutableArray *arr = [[[feed valueForKey:@"comments"]valueForKey:@"data" ]valueForKey:@"id"];
for (NSString* cid in [arr objectAtIndex:0]) {
    NSLog(@"cid is : %@ \n\n",cid);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文