【发布时间】:2013-11-14 10:51:17
【问题描述】:
我想从 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 存储[1226:70b] resultFetch: ( " (实体: 类型; id: 0x8a83e50 ; 数据: )", " (实体: 类型; id: 0x8a6ae80 ; 数据: )", “(实体:类型;id:0x8aa9650;数据:)” )
2013-11-14 10:31:53.107 存储[1226:70b] 结束 ViewDidLoad
2013-11-14 10:31:57.148 Storage[1226:70b] -[Type length]:无法识别的选择器发送到实例 0x8aa08e0
它不显示数据库内的类型名称。 你能帮我正确获取这个“表”以将这些属性字符串放在 UIPickerView 中吗?我想要的只是一个我可以使用的漂亮的 NSMutableArray! :)
非常感谢您的帮助。
宝拉
【问题讨论】:
-
你在哪里使用'type.length'?这就是应用程序崩溃的原因。您正在访问您的托管对象“类型”,但该对象没有称为“长度”的属性/方法。假设您需要获取对象的数量,您可能需要将 'type.length' 替换为 'resultFetch.count'。
-
也可以替换 NSLog(@"resultFetch: %@", resultFetch); by NSLog(@"resultFetch: %@", t.name);记录实际的字符串值。
-
嗨 Leijonien。我更改了 NSLog,现在我可以看到 Type 值。我的代码中看不到任何带有 lenght 字样的内容! ://
-
我不明白为什么如果我将 fetch 更改为一个简单的数组它工作正常!!!呸呸呸呸 :(
-
你能添加一个异常断点来检查它实际崩溃的地方吗?
标签: core-data nsmutablearray uipickerview nsfetchrequest