我正在尝试使用 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
实现文件如下所示,
视图控制器用户.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” 首先抛出调用堆栈: