我需要NSDictionary一點幫助。我怎樣才能獲得1對,可以說一個值「ID」如果我有字典如何從NSDictionary獲取鍵/值對?
NSDictionary *allCourses = [NSJSONSerialization JSONObjectWithData:allCoursesData options:NSJSONReadingMutableContainers error:&error];
,它看起來像這樣:
感謝您的幫助。
我需要NSDictionary一點幫助。我怎樣才能獲得1對,可以說一個值「ID」如果我有字典如何從NSDictionary獲取鍵/值對?
NSDictionary *allCourses = [NSJSONSerialization JSONObjectWithData:allCoursesData options:NSJSONReadingMutableContainers error:&error];
,它看起來像這樣:
感謝您的幫助。
的捷徑:
NSNumber *number = allCourses[@"semacko"][@"id"];
NSDictionary *semacko = [allCourses objectForKey:@"semacko"];
NSNumber *number = [semacko objectForKey:@"id"];
NSNumber *number = allCourses[@"semacko"][@"id"];
,或者如果你想要遍歷所有對象:
for(NSDictionary* course in allCourses) {
NSNumber *number = course[@"id"];
}
試試這個:
NSDictionary *allCourses = [NSJSONSerialization JSONObjectWithData:allCoursesData options:NSJSONReadingMutableContainers error:&error];
[allCourses enumerateKeysAndObjectsUsingBlock: ^(id key, id obj, BOOL *stop) {
// do something with key and obj
}];
NSString *name =[[NSString alloc]initWithFormat:@"%@",[Dictionaryname objectForKey:@"key"]];
這段代碼可以訪問對應的是,在objectforkey關鍵字指定的鍵的值
你可以試試:
NSNumber *courseID = [allCourses valueForKeyPath:@"semacko.id"];
對於設定值:
- setValue:forKeyPath:
你能添加一些細節,使這個答案更容易消化整個社區? – Werner