无法通过 nsdictionary 中的键访问值
Cannot access value through keys in nsdictionary
我很困惑这是什么问题,我的字典是:
eventDetails = (WebHandler.sharedInstance.eventsDictionary?.copy())! as! NSDictionary
print("Printing eventdeatails: '\(eventDetails)'")
Printing event details: '{
0 = {
"_id" = 5661958b2c0fee491a9e9e08;
date = "9am - 1pm";
degree = ee;
eventCategory = sports;
eventDescription = "cricket match between east and west wing";
eventPhoto = "";
eventTitle = "cricket match";
location = "west wing hall";
society = "the society that has nothing better to do";
universityId = ucf;
};
3 = {
"_id" = 5661981b69439a6a1b17870e;
date = "8am - 3pm";
degree = ee;
eventCategory = sports;
eventDescription = "football match between batch 13 and 14";
eventPhoto = "";
eventTitle = "football match";
location = "west wing hall";
society = "King KOng";
universityId = ucf;
};
1 = {
"_id" = 566195a72c0fee491a9e9e09;
date = "8am - 3pm";
degree = ee;
eventCategory = sports;
eventDescription = "football match between batch 13 and 14";
eventPhoto = "";
eventTitle = "football match";
location = "west wing hall";
society = "King KOng";
universityId = ucf;
};
2 = {
"_id" = 566195b12c0fee491a9e9e0a;
date = "8am - 3pm";
degree = ee;
eventCategory = entertainment;
eventDescription = "showing the harry potter!";
eventPhoto = "";
eventTitle = "movie showing";
location = "west wing hall";
society = "the society that has nothing better to do";
universityId = ucf;
};
}'
这就是我从网络处理程序中获取它的方式 class。我已经设置了非常简单的键 0、1、2,...只是为了在需要时轻松获取。
它正确地打印了完整的字典,但是每当我尝试访问它的值时,它让我得到那些特定的值,而不是我得到 'nil'
结构是我有 dict 和 dict 我现在尝试的是
let key = "2"
print(eventDetails[key]!) //not working
print(eventDetails["2"]!) // just for confirmation, not working
print(eventDetails["2"]!["_id"]!) // not working
print(eventDetails.valueForKey(idnumber)) // i have doubt on word "key" so i changed it and observed it but not good for me
请帮我推荐一些好的读物或一些我可以找到基础知识或给我一些出路的东西。我此刻一无所知。
提前致谢!
您的字典以整数值作为键,因此您必须使用 NSInteger(或 Int)类型的键访问字典。尝试:
let key = 2
print(eventDetails[key]!)
print(eventDetails[2]!)
我很困惑这是什么问题,我的字典是:
eventDetails = (WebHandler.sharedInstance.eventsDictionary?.copy())! as! NSDictionary
print("Printing eventdeatails: '\(eventDetails)'")
Printing event details: '{
0 = {
"_id" = 5661958b2c0fee491a9e9e08;
date = "9am - 1pm";
degree = ee;
eventCategory = sports;
eventDescription = "cricket match between east and west wing";
eventPhoto = "";
eventTitle = "cricket match";
location = "west wing hall";
society = "the society that has nothing better to do";
universityId = ucf;
};
3 = {
"_id" = 5661981b69439a6a1b17870e;
date = "8am - 3pm";
degree = ee;
eventCategory = sports;
eventDescription = "football match between batch 13 and 14";
eventPhoto = "";
eventTitle = "football match";
location = "west wing hall";
society = "King KOng";
universityId = ucf;
};
1 = {
"_id" = 566195a72c0fee491a9e9e09;
date = "8am - 3pm";
degree = ee;
eventCategory = sports;
eventDescription = "football match between batch 13 and 14";
eventPhoto = "";
eventTitle = "football match";
location = "west wing hall";
society = "King KOng";
universityId = ucf;
};
2 = {
"_id" = 566195b12c0fee491a9e9e0a;
date = "8am - 3pm";
degree = ee;
eventCategory = entertainment;
eventDescription = "showing the harry potter!";
eventPhoto = "";
eventTitle = "movie showing";
location = "west wing hall";
society = "the society that has nothing better to do";
universityId = ucf;
};
}'
这就是我从网络处理程序中获取它的方式 class。我已经设置了非常简单的键 0、1、2,...只是为了在需要时轻松获取。 它正确地打印了完整的字典,但是每当我尝试访问它的值时,它让我得到那些特定的值,而不是我得到 'nil' 结构是我有 dict 和 dict 我现在尝试的是
let key = "2"
print(eventDetails[key]!) //not working
print(eventDetails["2"]!) // just for confirmation, not working
print(eventDetails["2"]!["_id"]!) // not working
print(eventDetails.valueForKey(idnumber)) // i have doubt on word "key" so i changed it and observed it but not good for me
请帮我推荐一些好的读物或一些我可以找到基础知识或给我一些出路的东西。我此刻一无所知。 提前致谢!
您的字典以整数值作为键,因此您必须使用 NSInteger(或 Int)类型的键访问字典。尝试:
let key = 2
print(eventDetails[key]!)
print(eventDetails[2]!)