使用带Picker的嵌套NSDictionary



新手来了,

我从plist中设置了一个组件选择器,每个项都是一个数组,每个数组中有多个字符串,应用程序使用。

当前的plist结构是这样的:

NSDictionary  ->  NSArray  ->  NSString
                     |             |
            Items in Picker     Data for each Item

但是现在,我想:

NSDictionary  ->  NSDictionary  ->  NSArray  ->  NSString
                      |                |              |
  DIfferent Picker Data Sets      Items in Picker   Data for each Item

那么现在将有多组选择器组件我将使用分段控制等来显示。

我甚至不确定这是否可能,我只希望它能使我免于制作许多不同的单独的选择器控制器。

让我难倒的是让所有的东西都被正确地摄入

这就是我现在所拥有的,它成功构建但崩溃(调试信息如下):

NSBundle *bundle = [NSBundle mainBundle];
NSString *plistPath = [bundle pathForResource:@"CamerasDatabase" ofType:@"plist"];
NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
self.allCameras = dictionary;
[dictionary release];
NSArray *cameraTypes = [self.allCameras allKeys];
self.CamTypes = cameraTypes;
NSArray *items = [self.CamTypes objectAtIndex:0];
self.Cameras = items;
NSString *selectedCamera = [self.Cameras objectAtIndex:0];
NSArray *array = [CamsList objectForKey:selectedCam];
self.cameraData = array;

我已经尝试了许多不同的字典、数组和字符串的组合,所以我确定上面的代码是混乱的。

它崩溃在:

NSString *selectedCamera = [self.Cameras objectAtIndex:0];

使用"-[NSCFString objectAtIndex:]:无法识别的选择器发送到实例0x4e127f0"

很明显,你在self.CamTypes中有NSString对象(键)。不是NSArray。

所以这些行是无效的

NSArray *items = [self.CamTypes objectAtIndex:0];
self.Cameras = items;
NSString *selectedCamera = [self.Cameras objectAtIndex:0]; //this line is cause of exception.

修复这个问题,写一些像这样的代码

NSBundle *bundle = [NSBundle mainBundle];
NSString *plistPath = [bundle pathForResource:@"CamerasDatabase" ofType:@"plist"];
NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
self.allCameras = [dictionary valueForKey:@"Key "];//Key for accesing your inner dictionary
[dictionary release];

NSArray *cameraTypes = [self.allCameras allKeys];
self.CamTypes = cameraTypes;

NSArray *items = [self.allCameras valueForKey:[self.CamTypes objectAtIndex:0]];
self.Cameras = items;
NSString *selectedCamera = [self.Cameras objectAtIndex:0];
NSArray *array = [CamsList objectForKey:selectedCam];
self.cameraData = array;

so在上面的代码self。

是一个字典,它有数组对应不同的键(cameraTypes在self.CamTypes中)。

最新更新