Search code examples
iosjsonuipickerview

Another JSON Data in UIPickerView


I've taken a look a few other examples but I still can't seem to figure out why my pickerView stays empty.

JSON File

{
"artstyle": [
             {
             "id": 1001,
             "name": "Plain",
             "prefix": "plain"
             },
             {
             "id": 1002,
             "name": "Brushed",
             "prefix": "brushed"
             },
             {
             "id": 1003,
             "name": "Swirly",
             "prefix": "swirly"
             }
             ]}

.h

@interface FlipScrollViewController : UIViewController <UIPickerViewDataSource,UIPickerViewDelegate>

@property NSArray *artArray;

@end

.m

@interface FlipScrollViewController ()

@end

@implementation FlipScrollViewController


- (void)viewDidLoad
{
    [super viewDidLoad];

    //Getting the file path from the local bundle.
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"art" ofType:@"json"];

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSData* data = [NSData dataWithContentsOfFile:filePath];
        [self performSelectorOnMainThread:@selector(fetchedData:)
                               withObject:data waitUntilDone:YES];
    });

}
- (void)fetchedData:(NSData *)responseData {
    //parse out the json data
    NSError* error;


    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];

    //self.artArray = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
    //NSLog(@"get: %@", _artArray);


   //NSArray* shapeguide = [json objectForKey:(@"artstyle")];
    // this spits out the whole JSON file contents
    //NSLog(@"Guide: %@", shapeguide);

    //NSDictionary *art = [shapeguide objectAtIndex:0];
    //NSLog(@"Guide: %@", guide);
    // Displays the "name" value in the Label text

    //name.text = [NSString stringWithFormat:@"%@",[guide objectForKey:@"name"]];

    //self.artArray = [art objectForKey:@"name"];

  _artArray = [json objectForKey:(@"name")];
    //NSLog(@"Guide: %@", _artArray);
}

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

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

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

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

{
    //to do
    return [self.artArray count];
}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{

    return [[self.artArray objectAtIndex:row] valueForKey:@"name"];

}
 */

@end

I know I have set the delegates correctly and I know the JSON data is being spit out correctly. I've put NSLogs in at various place to check. You can see from parts of the .m that I have tried a bunch of different ways to get this thing working.

I've spent a few days on this now and I'm running out of hair to pull. Plus it seems bloated so any tips to clean it up would be appreciated. Thanks


Solution

  • - (void)fetchedData:(NSData *)responseData {
    //parse out the json data
    NSError* error;
    
    
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
    
    NSLog(@"Guide: %@", json);
    
    _artArray = [json objectForKey:@"artstyle"];
    
    [<picker> reloadAllComponents];
    }
    
    -(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    NSDictionary *dict = [_artArray objectAtIndex:row];
    NSLog(@"dict: %@", dict);
    return [dict objectForKey:@"name"];
    }