我想从我的Main.xcdatamodeld(coredata)中的“table”中检索一个类型列表,以放入UIPickerView。此实体“类型”只有一个属性“名称”(字符串)。
如果我在NSMutableArray中添加一些字符串,它就可以完美地运行。 UIPickerView显示字符串。上面是简单的NSMutableArray。
resultFetch = [[NSMutableArray alloc]initWithObjects:@"Electronics",@"School",@"Kitchen",@"Office", nil];
但是当我从coredata中取出时,我得到一个错误。这是下面的代码。
AddItemViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
nameField.text = [self.currentItem name];
locationField.text = [self.currentItem location];
//typeField.text = [self.currentItem type];
quantityField.text = [[self.currentItem quantity] stringValue];
self.nameField.delegate = self;
self.locationField.delegate = self;
//self.typeField.delegate = self;
self.quantityField.delegate = self;
NSLog(@"ViewDidload before put fetch in array");
//Put fetch in NSArray resultFetch
AppDelegate *myApp = (AppDelegate *) [[UIApplication sharedApplication]delegate];
resultFetch = [[NSMutableArray alloc] init];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Type" inManagedObjectContext:[myApp managedObjectContext]];
[fetchRequest setEntity:entity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
NSError *error;
NSArray *fetchedObjects = [[myApp managedObjectContext] executeFetchRequest:fetchRequest error:&error];
for (Type *t in fetchedObjects) {
[resultFetch addObject:t];
NSLog(@"resultFetch: %@", resultFetch);
}
//Define typePicker
typePicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0,43,320,480)];
typePicker.delegate = self;
typePicker.dataSource = self;
[typePicker setShowsSelectionIndicator:YES];
typeField.inputView = typePicker;
//Create done button in UIPickerView
myPickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 56)];
myPickerToolbar.barStyle = UIBarStyleBlackOpaque;
[myPickerToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(pickerDoneClicked)];
[barItems addObject:doneBtn];
[myPickerToolbar setItems:barItems animated:YES];
typeField.inputAccessoryView = myPickerToolbar;
NSLog(@"End ViewDidLoad");
}
-(void)pickerDoneClicked
{
NSLog(@"Done Clicked");
[typeField resignFirstResponder];
myPickerToolbar.hidden=YES;
typePicker.hidden=YES;
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [resultFetch count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [resultFetch objectAtIndex:row];
}
- (void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
typeField.text = [resultFetch objectAtIndex:row];
}
错误是:
2013-11-14 10:31:53.099存储[1226:70b]在数据库中提取之前的ViewDidload
2013-11-14 10:31:53.102存储[1226:70b] resultFetch :( “(entity:Type; id:0x8a83e50; data:)” )
2013-11-14 10:31:53.103存储[1226:70b] resultFetch :( “(entity:Type; id:0x8a83e50; data:)”, “(entity:Type; id:0x8a6ae80; data :))” )
2013-11-14 10:31:53.104存储[1226:70b] resultFetch :( “(entity:Type; id:0x8a83e50; data:)”, “(entity:Type; id:0x8a6ae80; data :)), “(entity:Type; id:0x8aa9650; data:)” )
2013-11-14 10:31:53.107存储[1226:70b]结束ViewDidLoad
2013-11-14 10:31:57.148存储[1226:70b] - [类型长度]:无法识别的选择器发送到实例0x8aa08e0
它不显示数据库中的类型名称。 你能帮我正确地获取这个“表”来将这些属性字符串放在UIPickerView中吗?我想要的只是一个可以使用的漂亮的NSMutableArray! :)
非常感谢你的帮助。
保
答案 0 :(得分:1)
问题解决了!从[resultFetch addObject:t]更改了for语句中的行;到[resultFetch addObject:t.name] ;. 好像垃圾太多了! :)