简体   繁体   English

从NSDictionary解析NSString值返回nil

[英]Parsing a NSString value from NSDictionary returns nil

I am trying to parse the values from an NSDictionary into a local data model. 我正在尝试将NSDictionary中的值解析为本地数据模型。

Here are the contents of the dictionary. 这是字典的内容。

Printing description of userProfile:
{
    "created_at" = "2014-01-16T09:08:03.012Z";
    "first_name" = FirstName;
    id = 74;
    "last_name" = LastName;
    phone = 09876543232;
    photo = "<null>";
    "updated_at" = "2014-01-16T12:40:18.143Z";
    "user_id" = 84;
}

This is what i use to parse the strings: 这是我用来解析字符串的内容:

self.profileId = [[userProfile objectForKey:@"id"] intValue];
self.firstName = (NSString*)[userInfo objectForKey:@"first_name"];
self.lastName = (NSString*)[userInfo objectForKey:@"last_name"];
self.phoneNumber = (NSString*)[userInfo objectForKey:@"phone"];

This will return nil on the last three fields. 这将在最后三个字段上返回nil。 However, if i use this method to parse the data it will actually work. 但是,如果我使用此方法来解析数据,它将真正起作用。

NSArray*keys=[userProfile allKeys];
self.firstName = [userProfile objectForKey:[keys objectAtIndex:3]];

Any idea why this is happening? 知道为什么会这样吗?

You are accessing 'userProfile' here 您正在此处访问“ userProfile”

self.profileId = [[userProfile objectForKey:@"id"] intValue];

but accessing 'userInfo' here 但在这里访问“ userInfo”

self.firstName = (NSString*)[userInfo objectForKey:@"first_name"];
self.lastName = (NSString*)[userInfo objectForKey:@"last_name"];
self.phoneNumber = (NSString*)[userInfo objectForKey:@"phone"];

Is this a typo or do you have an object called 'userInfo' that you are mistakenly using? 这是拼写错误,还是您有误使用的名为“ userInfo”的对象?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM