0

I have a plist that I want to get the contents of in a uipickerview.

I'm able to output the contents of the plist in the console using:

    // Path to the plist (in the application bundle)
    NSString *path = [[NSBundle mainBundle] pathForResource:
                                        @"loa" ofType:@"plist"];

    // Build the array from the plist  
    NSMutableArray *array2 = [[NSMutableArray alloc] initWithContentsOfFile:path];

    // Show the string values  
    for (NSString *str in array2)
            NSLog(@"--%@", str);

what I need to figure out is how to get this content into the UIPickerView.

I tried the following:

        arrayPicker = [[NSMutableArray alloc] initWithArray:array2];

, but get an error: exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary isEqualToString:]: unrecognized selector sent to instance

thanks for any help

hanumanDev
  • 6,592
  • 11
  • 82
  • 146

2 Answers2

1

What you should do is:

NSMutableDictionary *myDataDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
NSLog(@"MyDict:%@",[myDataDictionary description]);

Your plist file is an NSDictionary;

Alex Terente
  • 12,006
  • 5
  • 51
  • 71
1

You've got to use the picker delegate/datasource

First, make the array a property loaded in the viewDidLoad, i.e.:

-(void)viewDidLoad {
NSString *path = [[NSBundle mainBundle] pathForResource:
                                        @"loa" ofType:@"plist"];
self.myArray = [[NSArray alloc] initWithContentsOfFile:path];
}

THEN

Conform your class to picker delegate/datasource, and hook it up in IB.

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return [myArray count];
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [myArray objectAtIndex:row];
}

What is the structure of your plist? - Is it just a flat array, or an array assigned to a one-key dict, or a dict with key/object pairs...

Alex Coplan
  • 13,211
  • 19
  • 77
  • 138
  • the plist is an Array with a Dictionary and 4 strings within the dictionary for each item. I tried something similar to what you have above but kept getting the error "myArray" undeclared. – hanumanDev Jul 13 '11 at 15:14