尝试访问值时出现 Nsdictionary 键值编码错误
Nsdictionay keyvalue coding error when trying to access the values
{
"gender" : "Male",
"city" : "chenna",
"instituteName" : "srm",
"profileImage" : "ProfileImage",
"dob" : "DOB",
"emailAddress" : "saxasx",
"isStudent" : "true",
"studentIdValidity" : "Month2Year5",
"studentIdImage" : "IDImage",
"name" : "xasx",
"phoneNumber" : "9092256227"
}
NSDictionary *dic ;
dic = [[NSUserDefaults standardUserDefaults]objectForKey:@"fizzstudentjson"];
NSLog(@"%@",dic);
NSString *strng = [dic objectForKey:@"city"];
NSLog(@"%@",strng);
当我尝试访问字符串中的字典值时我的应用程序崩溃了
这是我写的代码帮助我
这对我有用。
NSDictionary *dictionary = @{
@"gender" : @"Male",
@"city" : @"chenna",
@"instituteName" : @"srm",
@"profileImage" : @"ProfileImage",
@"dob" : @"DOB",
@"emailAddress" : @"saxasx",
@"isStudent" : @"true",
@"studentIdValidity" : @"Month2Year5",
@"studentIdImage" : @"IDImage",
@"name" : @"xasx",
@"phoneNumber" : @"9092256227"
};
[[NSUserDefaults standardUserDefaults] setObject:dictionary forKey:@"fizzstudentjson"];
NSDictionary *dic = [[NSUserDefaults standardUserDefaults]objectForKey:@"fizzstudentjson"];
NSLog(@"%@",dic);
NSString *strng = [dic valueForKey:@"city"];
NSLog(@"%@",strong);
结果:chenna
as Log about data 它可能是 NSString 格式,而不是 NSDictionary。所以你需要在使用前将它转换为 NSDictionary。试试这个代码
NSString *fizzstudentjson = [[NSUserDefaults standardUserDefaults]objectForKey:@"fizzstudentjson"];
NSData *data = [fizzstudentjson dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSString *city = dictionary[@"city"];
{
"gender" : "Male",
"city" : "chenna",
"instituteName" : "srm",
"profileImage" : "ProfileImage",
"dob" : "DOB",
"emailAddress" : "saxasx",
"isStudent" : "true",
"studentIdValidity" : "Month2Year5",
"studentIdImage" : "IDImage",
"name" : "xasx",
"phoneNumber" : "9092256227"
}
NSDictionary *dic ;
dic = [[NSUserDefaults standardUserDefaults]objectForKey:@"fizzstudentjson"];
NSLog(@"%@",dic);
NSString *strng = [dic objectForKey:@"city"];
NSLog(@"%@",strng);
当我尝试访问字符串中的字典值时我的应用程序崩溃了 这是我写的代码帮助我
这对我有用。
NSDictionary *dictionary = @{
@"gender" : @"Male",
@"city" : @"chenna",
@"instituteName" : @"srm",
@"profileImage" : @"ProfileImage",
@"dob" : @"DOB",
@"emailAddress" : @"saxasx",
@"isStudent" : @"true",
@"studentIdValidity" : @"Month2Year5",
@"studentIdImage" : @"IDImage",
@"name" : @"xasx",
@"phoneNumber" : @"9092256227"
};
[[NSUserDefaults standardUserDefaults] setObject:dictionary forKey:@"fizzstudentjson"];
NSDictionary *dic = [[NSUserDefaults standardUserDefaults]objectForKey:@"fizzstudentjson"];
NSLog(@"%@",dic);
NSString *strng = [dic valueForKey:@"city"];
NSLog(@"%@",strong);
结果:chenna
as Log about data 它可能是 NSString 格式,而不是 NSDictionary。所以你需要在使用前将它转换为 NSDictionary。试试这个代码
NSString *fizzstudentjson = [[NSUserDefaults standardUserDefaults]objectForKey:@"fizzstudentjson"];
NSData *data = [fizzstudentjson dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSString *city = dictionary[@"city"];