使用NSArray使用核心数据DB的结果填充uipicker视图

时间:2013-06-26 22:05:44

标签: ios objective-c core-data uipickerview

我正在尝试使用NSFetchRequest的结果填充UIPickerView。然后将NSFetchRequest的结果存储在NSArray中。我正在使用Core Data与SQLite DB进行交互。我有一个简单的类文件,其中包含与项目中的故事板场景相关联的UIPickerView。

该类的头文件如下所示

ViewControllerUsers.h

#import <UIKit/UIKit.h>
#import "AppDelegate.h"

@interface ViewControllerUsers : UIViewController <NSFetchedResultsControllerDelegate, UIPickerViewDelegate, UIPickerViewDataSource>
{

NSArray *dictionaries;
}
@property (nonatomic, strong) NSFetchedResultsController *fetchedResultsController;
// Core Data
@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;

@property (nonatomic, strong) NSArray *users;

@property (strong, nonatomic) IBOutlet UIPickerView *uiPickerViewUsers;

@property (weak, nonatomic) IBOutlet UIBarButtonItem *btnDone;

@property (weak, nonatomic) IBOutlet UIButton *btnChangePin;

// added for testing purposes
@property (nonatomic, strong) NSArray *usernames;

- (IBAction)dismissScene:(id)sender;

- (IBAction)changePin:(id)sender;

@end

实现文件如下所示,

ViewControllerUsers.m

#import "ViewControllerUsers.h"

@interface ViewControllerUsers ()

@end

@implementation ViewControllerUsers

// Core Data
@synthesize managedObjectContext = _managedObjectContext;

@synthesize uiPickerViewUsers = _uiPickerViewUsers;

@synthesize usernames = _usernames;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    // Core Data
    if (_managedObjectContext == nil)
    {
        _managedObjectContext = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
        NSLog(@"After _managedObjectContext: %@",  _managedObjectContext);
    }

    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Account"];

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Account" inManagedObjectContext:_managedObjectContext];
    request.resultType = NSDictionaryResultType;
    request.propertiesToFetch = [NSArray arrayWithObject:[[entity propertiesByName] objectForKey:@"username"]];
    request.returnsDistinctResults = YES;

    _usernames = [_managedObjectContext executeFetchRequest:request error:nil];

    NSLog (@"names: %@",_usernames);
}


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

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    //set number of rows
    return _usernames.count;
}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    //set item per row
    return [_usernames objectAtIndex:row];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidUnload {
    [self setBtnDone:nil];
    [self setUiPickerViewUsers:nil];
    [self setBtnChangePin:nil];
    [super viewDidUnload];
}
- (IBAction)dismissScene:(id)sender {

    [self dismissModalViewControllerAnimated:YES];
}

- (IBAction)changePin:(id)sender {
}
@end

当前代码导致应用程序崩溃,但NSLog会在NSArray中显示NSFetchRequest的结果。我目前认为如果我不得不猜测,我没有正确格式化NSArray中NSFetchRequest的结果。

崩溃日志如下所示,

2013-06-26 16:49:24.219 KegCop [41233:c07]姓名:(         {         username = blah;     },         {         username = chris;     },         {         username = root;     } ) 2013-06-26 16:49:24.223 KegCop [41233:c07] - [NSKnownKeysDictionary1 isEqualToString:]:无法识别的选择器发送到实例0xe54d9a0 2013-06-26 16:49:24.223 KegCop [41233:c07] 由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:' - [NSKnownKeysDictionary1 isEqualToString:]:无法识别的选择器发送到实例0xe54d9a0' 首先抛出调用堆栈:

1 个答案:

答案 0 :(得分:0)

您的usernames数组是一个字典数组,而不是字符串数组。

更新您的titleForRow:方法,如下所示:

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    //set item per row
    return _usernames[row][@"username"];
}