Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何从目标C中的NSDictionary中获取键值?_Ios_Objective C - Fatal编程技术网

Ios 如何从目标C中的NSDictionary中获取键值?

Ios 如何从目标C中的NSDictionary中获取键值?,ios,objective-c,Ios,Objective C,我有一本字典: { "FirstName" : "Katie", "SecondName" : "Brown" } 如何从上述字典中获取键值“FirstName”和“SecondName”您可以通过以下方式获得 if ([yourDictname objectForKey:@"FirstName"] && [yourDictname objectForKey:@"SecondName"]) { // key exists. } else { // ...

我有一本字典:

{
  "FirstName" : "Katie",
  "SecondName" : "Brown"
}
如何从上述字典中获取键值“FirstName”和“SecondName”

您可以通过以下方式获得

if ([yourDictname objectForKey:@"FirstName"] && [yourDictname objectForKey:@"SecondName"]) {
    // key exists.
}
else
{
    // ...
}
或者你可以

if ([[yourDictname allKeys] containsObject:@"FirstName"] && [[yourDictname allKeys] containsObject:@"SecondName"]) {
    // key exists.
}
请尝试以下代码:

NSData* yourJSONData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];

NSDictionary *jsonObject = [NSJSONSerialization JSONObjectWithData:yourJSONData options:0 error:nil];
NSArray *allTheKeys = [jsonObject allKeys];

您可以从字典中获取密钥数组,如

   NSDictionary *dic; // your dictionary here

NSArray *allKeys = [dic allKeys];
NSLog(@"all keys : %@",allKeys);
您也可以为值获得相同的值,如
[dic allValues]

NSString *jsonString = @" {\"FirstName\" : \"Katie\",\"SecondName\" : \"Brown\"}";
NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
for (NSString *key in dic.allKeys) {
    NSLog(@"key: %@ value: %@", key, dic[key]);
}

或者重试。

不显示为有效的JSONif([dict objectForKey:@“Firstname”!=nil){//key not null}可以使用NSArray*arrKey=[dict allkey];NSArray*arrVal=[dict allValues]@Shubhank我没有从谷歌找到答案,请对你的答案进行解释