NSDictionary objectForKey 抛出异常 NSCFString

发布于 2024-12-11 11:39:12 字数 1409 浏览 0 评论 0原文

这是我的代码:

    NSError *error = nil;
    SBJsonParser *parserJson = [[SBJsonParser alloc] init];
    NSDictionary *jsonObject = [parserJson objectWithString:webServiceResponse error:&error]; 
    [parserJson release], parserJson = nil;

    //Test to see if response is different from nil, if is so the parsing is ok
    if(jsonObject != nil){
        //Get user object
        NSDictionary *userJson = [jsonObject objectForKey:@"LoginPOST2Result"];
        if(userJson != nil){
            self.utente = [[User alloc] init];
            self.utente.userId = [userJson objectForKey:@"ID"];
        }

虽然 Json 字符串 webServiceResponse 是:

{"LoginPOST2Result":
    "{\"ID\":1,
    \"Username\":\"Pippo\",
    \"Password\":\"Pippo\",
    \"Cognome\":\"Cognome1\",
    \"Nome\":\"Nome1\",
    \"Telefono\":\"012345678\",
    \"Email\":null,
    \"BackOffice\":true,
    \"BordoMacchina\":false,
    \"Annullato\":false,
    \"Badge\":1234}"
}

执行此行时出现问题:

self.utente.userId = (NSInteger *) [userJson objectForKey:@"ID"];

并且错误是:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString objectForKey:]: unrecognized selector sent to instance 0x6861520'

该错误似乎是由于对象 userJson 不是 NSDictionary 而是 NSCFString 类型,因此不响应消息objectForKey:.
我哪里做错了?

This is my code:

    NSError *error = nil;
    SBJsonParser *parserJson = [[SBJsonParser alloc] init];
    NSDictionary *jsonObject = [parserJson objectWithString:webServiceResponse error:&error]; 
    [parserJson release], parserJson = nil;

    //Test to see if response is different from nil, if is so the parsing is ok
    if(jsonObject != nil){
        //Get user object
        NSDictionary *userJson = [jsonObject objectForKey:@"LoginPOST2Result"];
        if(userJson != nil){
            self.utente = [[User alloc] init];
            self.utente.userId = [userJson objectForKey:@"ID"];
        }

While Json string webServiceResponse is:

{"LoginPOST2Result":
    "{\"ID\":1,
    \"Username\":\"Pippo\",
    \"Password\":\"Pippo\",
    \"Cognome\":\"Cognome1\",
    \"Nome\":\"Nome1\",
    \"Telefono\":\"012345678\",
    \"Email\":null,
    \"BackOffice\":true,
    \"BordoMacchina\":false,
    \"Annullato\":false,
    \"Badge\":1234}"
}

The problem arise when is execute this line:

self.utente.userId = (NSInteger *) [userJson objectForKey:@"ID"];

and the error is:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString objectForKey:]: unrecognized selector sent to instance 0x6861520'

The error seems to be due to the fact that the object userJson is not an NSDictionary but rather NSCFString type and therefore does not respond to the message objectForKey:.
Where am I doing wrong?

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

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

发布评论

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

评论(2

九歌凝 2024-12-18 11:39:12

问题在于,虽然键“LoginPOST2Result”的 json 响应中的值看起来像字典,但它实际上是一个字符串,因为它用引号引起来。

因此,您将 objectForKey: 消息发送到 NSString 而不是 NSDictionary。 NSString 不响应 objectForKey:。

看起来 webServiceResponse 生成不正确或解析不正确。

The problem is that whilst the value in the json response for the key "LoginPOST2Result" looks like a dictionary, it is actually a string as it is enclosed in quotes.

So you are sending the objectForKey: message to an NSString and not an NSDictionary. NSString does not respond to objectForKey:.

It looks like the webServiceResponse is being generated incorrectly or parsed incorrectly.

救赎№ 2024-12-18 11:39:12

您需要更好地理解 Cocoa 框架中什么是指针,什么不是。

事实上,您将 userJson 定义为 NSDictionary 而不是 NSDictionary *。考虑到 Cocoa 中的所有对象都是指针。事实上检查 [NSDictionary objectForKey:] 返回一个“id”,然后您必须使用 NSDictionary *。只需使用 NSDictionary 您将引用该类。

稍后在转换为 (NSInteger *) 时也会犯类似的错误,但是 NSInteger (NSInteger 不是一个对象,它是一个基本类型,从 long 或 int (取决于平台架构)犹豫不决,正如您从其定义中看到的那样:


#if __LP64__ || TARGET_OS_EMBEDDED || TARGET_OS_IPHONE || TARGET_OS_WIN32 || NS_BUILD_32_LIKE_64
typedef long NSInteger;
#else
typedef int NSInteger;
#endif

而且看起来从上面的对象定义来看,您尝试获取的密钥被转储为字符串,并且您正在尝试获取字典,请检查原始 json,它可能不是您期望的格式,

所以最后您至少有 。 3 个错误将导致您的应用程序崩溃。

You need to better understand what is a pointer and what no in the Cocoa Framework.

Infact you are defining userJson to NSDictionary and not NSDictionary *. Consider that all objects in Cocoa are pointers. Infact check that [NSDictionary objectForKey:] returns an "id" and then you must use NSDictionary *. Using simply NSDictionary you will refer to the class.

Similar mistake is done later in the cast to (NSInteger *) but NSInteger (NSInteger is not an object, it's a basic type hesitated from long or int (depends on the platform architecture) as you can see from its definition:


#if __LP64__ || TARGET_OS_EMBEDDED || TARGET_OS_IPHONE || TARGET_OS_WIN32 || NS_BUILD_32_LIKE_64
typedef long NSInteger;
#else
typedef int NSInteger;
#endif

And also it seems from the object definition above that the key you are trying to get is dumped as a string and you are trying to fetch a dictionary. Please check the original json which probably is not in the format you expect.

So at the end you have at least 3 errors that will make your app crash.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文