【发布时间】:2020-06-28 06:17:30
【问题描述】:
试图获取核心数据对象到tableview并得到Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unacceptable type of value for attribute: property = "seminar"; desired type = NSString; given type = UITextField;的异常
TeacherViewController.m 保存数据,获取并在TeacherDataViewController.m 的表格视图中显示它
**
TeacherViewController.h
**
- (IBAction)btnsubmit:(UIButton *)sender {
NSManagedObjectContext *context = [self managedobjectcontext];
NSManagedObject *new = [NSEntityDescription insertNewObjectForEntityForName:@"Teacher" inManagedObjectContext:context];
[new setValue:self.txtseminar forKey:@"seminar"];
[new setValue:self.txtassignment forKey:@"assignment"];
NSError *error = nil;
if (![context save:&error]) {
NSLog(@"can't save!! %@ %@", error, [error localizedDescription]);
}
}
**
TeacherDataViewController.m
**
- (void)viewDidLoad {
[super viewDidLoad];
myarray = [[NSMutableArray alloc]initWithObjects:([self.lblsemnr.text valueForKey:@"seminar"]), ([self.lblassmnt.text valueForKey:@"assignment"]), nil];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.devices.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellidentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellidentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
cell.textLabel.text = [myarray objectAtIndex:indexPath.row];
cell.detailTextLabel.text = [myarray objectAtIndex:indexPath.row];
return cell;
}
不保存任何数据并抛出上述异常
【问题讨论】:
标签: ios objective-c uitableview core-data