“-[__NSDictionaryI length]: unrecognized selector

2019-09-22 06:33发布

问题:

In my code, when I press a button to open a popup screen, it is supposed to take data from a website and store that in a string, and then put that string into a textview on the popup screen. When I run the program and press the button to get to the popup screen, I get a SIGABRT error and "-[__NSDictionaryI length]: unrecognized selector sent to instance". After researching this, I saw that the error comes when you try to reference NSDictionary to a length method. I don't have an NSDictionary in my code, so I don't know what causes the error.

Error Message

[76416:60b] -[NSDictionaryI length]: unrecognized selector sent to instance 0x8f8ece0 2014-08-13 16:41:59.248 CWSGui[76416:60b] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDictionaryI length]: unrecognized selector sent to instance 0x8f8ece0' * First throw call stack: ( 0 CoreFoundation 0x01c0d1e4 __exceptionPreprocess + 180 1 libobjc.A.dylib
0x0198c8e5 objc_exception_throw + 44 2 CoreFoundation
0x01caa243 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275 3
CoreFoundation 0x01bfd50b ___forwarding_
+ 1019 4 CoreFoundation 0x01bfd0ee _CF_forwarding_prep_0 + 14 5 Foundation 0x015aa4e4 -[NSConcreteAttributedString length] + 42 6 Foundation
0x015a9a6c -[NSConcreteAttributedString initWithString:attributes:] + 182 7 UIKit 0x00d5ae9d -[UITextView setText:] + 125 8 CWSGui 0x0000805e -[SystemLog viewDidAppear:] + 686 9 UIKit 0x0076f099 -[UIViewController _setViewAppearState:isAnimating:] + 526 10 UIKit 0x0076f617 -[UIViewController viewDidAppear:] + 146 11 UIKit 0x00771014 __64-[UIViewController viewDidMoveToWindow:shouldAppearOrDisappear:]_block_invoke + 44 12 UIKit 0x0076f9aa -[UIViewController _executeAfterAppearanceBlock] + 63 13 UIKit 0x0066a0d0 ___afterCACommitHandler_block_invoke_2 + 33 14 UIKit
0x0066a055 _applyBlockToCFArrayCopiedToStack + 403 15 UIKit
0x00669e9a _afterCACommitHandler + 568 16 CoreFoundation
0x01bd536e __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION
+ 30 17 CoreFoundation 0x01bd52bf __CFRunLoopDoObservers + 399 18 CoreFoundation 0x01bb3254 __CFRunLoopRun + 1076 19 CoreFoundation 0x01bb29d3 CFRunLoopRunSpecific + 467 20 CoreFoundation
0x01bb27eb CFRunLoopRunInMode + 123 21 GraphicsServices
0x03a3d5ee GSEventRunModal + 192 22 GraphicsServices
0x03a3d42b GSEventRun + 104 23 UIKit
0x0064cf9b UIApplicationMain + 1225 24 CWSGui
0x00008e2d main + 141 25 libdyld.dylib
0x024be701 start + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException

View Did Appear

- (void)viewDidAppear:(BOOL)animated {

    NSUserDefaults *ipdefaults = [NSUserDefaults standardUserDefaults];

    IPString = [ipdefaults objectForKey:@"IP"];
    NSString *http = @"http://";
    NSString *IPMiddle = [http stringByAppendingString:IPString];
    NSString *IPFinal = [IPMiddle stringByAppendingString:@"/tasks"];
    NSLog(@"Final IP: %@", IPFinal);

    //array stuff from the ip

    array = [[NSMutableArray alloc]init];

    //make a connection
    [self getIPWithIP:IPFinal];

    NSString *str=@"http://example.com";

    NSURL *url=[NSURL URLWithString:str];
    NSData *data=[NSData dataWithContentsOfURL:url];
    NSError *error=nil;
    NSString *string = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingMutableContainers error:&error];


    NSLog(@"Your JSON Object: %@", string);
    textField.text = string;
 } 

回答1:

[NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingMutableContainers error:&error];

is never returning a string. Either a NSArray or a NSDictionary. (The documentation only promises: A Foundation object from the JSON data in data, or nil if an error occurs. So it might be another type in future).

You error starts with

-[NSDictionaryI length]:

so in your case it is a dictionary (NSDictionaryI is a concrete immutable subclass of NSDictionary).

Just because you type the variable NSString it doesn't change the object assigned to it.



回答2:

You are trying to get the length of an NSString that is actually an NSDictionary. It's probably the result of some JSON parsing.