0

我想从 Main.xcdatamodeld (coredata) 中的“表”中检索类型列表以放入 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 Storage[1226:70b] ViewDidload before put fetch in array

2013-11-14 10:31:53.102 存储 [1226:70b] resultFetch: (“ (实体: 类型; id: 0x8a83e50 ; 数据:)”)

2013 年 11 月 14 日 10:31:53.103 存储[1226:70b] resultFetch:(“(实体:类型;ID:0x8a83e50;数据:)”,“(实体:类型;ID:0x8a6ae80;数据:)”)

2013-11-14 10:31:53.104 Storage[1226:70b] resultFetch: (" (entity: Type; id: 0x8a83e50 ; data: )", " (entity: Type; id: 0x8a6ae80 ; data: )", " (实体:类型;id:0x8aa9650;数据:)")

2013-11-14 10:31:53.107 存储 [1226:70b] 结束 ViewDidLoad

2013-11-14 10:31:57.148 存储[1226:70b]-[类型长度]:无法识别的选择器发送到实例 0x8aa08e0

它不显示数据库内的类型名称。你能帮我正确获取这个“表”以将这些属性字符串放在 UIPickerView 中吗?我想要的只是一个我可以使用的漂亮的 NSMutableArray!:)

非常感谢您的帮助。

保拉

4

1 回答 1

1

问题解决了!将 for 语句中的行从 [resultFetch addObject:t]; 到 [resultFetch addObject:t.name];。看来垃圾太多了!:)

于 2013-11-14T18:07:03.647 回答