开发者

NSDictionary parsing Exception in iphone

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

201开发者_运维技巧1-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
)


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


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


NSMutableArray *arr = [[[feed valueForKey:@"comments"]valueForKey:@"data" ]valueForKey:@"id"];
for (NSString* cid in [arr objectAtIndex:0]) {
    NSLog(@"cid is : %@ \n\n",cid);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜