Domanda

I'm confused on how to take a plist and use it as the datasource for a pickerview with 2 sections. My plist is a list of arrays. I want to populate a pickerview with section 1 as the key and then populate section 2 with the corresponding array.

I have the following plist format: enter image description here

I'm unsure of how to convert the plist into the structure I need to populate the pickerview. In my controller (viewDidLoad):

    // load plists
NSString *filmTypePath = [[NSBundle mainBundle] pathForResource:@"photographic-film" ofType:@"plist"];
filmTypesDict = [[NSDictionary alloc] initWithContentsOfFile:filmTypePath];

filmTypesArray = [NSMutableArray arrayWithCapacity:[filmTypesDict count]];

for(id key in [filmTypesDict keysSortedByValueUsingSelector:@selector(compare:)]){
    [filmTypesArray addObject:key];
    NSLog(@"%@",key);
}

which of course results in an error:

terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray compare:]: unrecognized selector sent to instance 0x16e7f3a0'

obviously I'm on the wrong track here, hoping for some direction...

È stato utile?

Soluzione

The way you try to create the sorted keys is incorrect. Try this:

NSString *filmTypePath = [[NSBundle mainBundle] pathForResource:@"photographic-film" ofType:@"plist"];
filmTypesDict = [[NSDictionary alloc] initWithContentsOfFile:filmTypePath];

filmTypesArray = [[filmTypesDict allKeys] sortedArrayUsingSelector:@selector(compare:)];

And there is no need for any of these arrays to be mutable.

The method you were using is trying to sort the keys based on their corresponding values. You simply want an alphabetic sort of the keys with no regard to their values.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top