NSDictionary在转换后显示为null

时间:2014-05-31 12:30:54

标签: ios objective-c json nsdictionary nslog

NSString *profilestring = [self.userDefaults objectForKey:@"profilestrings"];
NSDictionary *dicProfile = [profilestring propertyListFromStringsFileFormat];
NSLog(@"--->>%@",profilestring);
NSLog(@">>%@",dicProfile);

self.userDefaults是NSUserDefaults,NSLog(@"--->>%@",profilestring);打印一个字符串,例如

{
    useraddtime = 1401444211;
    userage = 11;
    usernickname = XJD;
    usersex = 1;
    userstatus = 1;
}

NSLog(@">>%@",dicProfile);打印(null),你能帮助我吗?

2 个答案:

答案 0 :(得分:1)

Apples文档说键和值本身总是用直引号括起来的字符串。您的密钥和值未包含在引号中。字符串即profilestring应如下所示

{
"useraddtime" = "1401444211";
"userage" = "11";
"usernickname" = "XJD";
"usersex" = "1";
"userstatus" = "1";

}

答案 1 :(得分:0)

您的[self.userDefaults objectForKey:@"profilestrings"]实际上是NSDictionary,因此您可以执行类似

的操作
NSDictionary *dicProfile = [NSDictionary dictionaryWithDictionary:[self.userDefaults objectForKey:@"profilestrings"]];