[英]Parsing a NSString value from NSDictionary returns nil
我正在嘗試將NSDictionary中的值解析為本地數據模型。
這是字典的內容。
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;
}
這是我用來解析字符串的內容:
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"];
這將在最后三個字段上返回nil。 但是,如果我使用此方法來解析數據,它將真正起作用。
NSArray*keys=[userProfile allKeys];
self.firstName = [userProfile objectForKey:[keys objectAtIndex:3]];
知道為什么會這樣嗎?
您正在此處訪問“ userProfile”
self.profileId = [[userProfile objectForKey:@"id"] intValue];
但在這里訪問“ userInfo”
self.firstName = (NSString*)[userInfo objectForKey:@"first_name"];
self.lastName = (NSString*)[userInfo objectForKey:@"last_name"];
self.phoneNumber = (NSString*)[userInfo objectForKey:@"phone"];
這是拼寫錯誤,還是您有誤使用的名為“ userInfo”的對象?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.