ios – – [__ NSCFArray objectForKeyedSubscript:]错误?

ios – – [__ NSCFArray objectForKeyedSubscript:]错误?,第1张

概述我在登录后试图从我的iOS应用程序中的 JSON响应对象获取数据.但我仍然收到此错误. 错误: 'NSInvalidArgumentException', reason: '-[__NSCFArray objectForKeyedSubscript:]: unrecognized selector sent to instance 0x8fc29b0' 这是我的请求代码,我正在使用AFNetwor 我在登录后试图从我的iOS应用程序中的 JSON响应对象获取数据.但我仍然收到此错误.

错误:

'NSinvalidargumentexception',reason: '-[__NSCFArray objectForKeyedSubscript:]: unrecognized selector sent to instance 0x8fc29b0'

这是我的请求代码,我正在使用AFNetworking:

self.operation = [manager GET:urlString parameters:params success:^(AFhttpRequestoperation *operation,ID responSEObject) {            NSDictionary *JsON = (NSDictionary *)responSEObject;            NSDictionary *user = JsON[@"user"];            Nsstring *token = user[@"auth_token"];            Nsstring *userID = user[@"ID"];//            Nsstring *avatarURL = user[@"avatar_url"];//            weakSelf.credentialStore.avatarURL = avatarURL;            weakSelf.credentialStore.authToken = token;            weakSelf.credentialStore.userID = userID;            weakSelf.credentialStore.username = self.usernameFIEld.text;            weakSelf.credentialStore.password = self.passwordFIEld.text;            [SVProgressHUD dismiss];            [self dismissVIEwControllerAnimated:YES completion:nil];        } failure:^(AFhttpRequestoperation *operation,NSError *error) {            if (operation.isCancelled) {                return;            }            [SVProgressHUD showErrorWithStatus:@"Login Failed"];            NSLog(@"%@",error);        }];

JsON响应对象的外观如下:

<__NSCFArray 0x8cac0b0>({    user =     {        "auth_token" = b3a18e0fb278739649a23f0ae325fee1e29fe5d6;        email = "[email protected]";        ID = 1;        username = jack;    };})

我正在使用像这样的指针将数组转换为Dictionary:

编辑:正如评论中指出的那样,任何其他人都会因为iOS知识有限而偶然发现.我在这里投掷不转换.有关完整说明,请参阅答案.

NSDictionary *JsON = (NSDictionary *)responSEObject;

我是iOS的新手,如果问题很明显就道歉.

谢谢你的帮助.

解决方法 你做的“转换”没有做任何转换,它是一个演员.这只是告诉编译器忽略它对这个对象知道的类型,并且就像它传递它的类型一样.

查看您拥有的输出,您不会返回字典,而是使用单个字典的字典数组.要访问第一个字典,您可以使用它而不是强制转换:

NSDictionary *JsON = [responSEObject objectAtIndex:0];

请注意,由于您从Web服务获取数据,因此您可能还应检查所获得的内容是否符合预期.

总结

以上是内存溢出为你收集整理的ios – – [__ NSCFArray objectForKeyedSubscript:]错误?全部内容,希望文章能够帮你解决ios – – [__ NSCFArray objectForKeyedSubscript:]错误?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1006197.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存