【问题标题】:'NSInvalidArgumentException' when trying to fetch core data objects to TableView尝试将核心数据对象提取到 TableView 时出现“NSInvalidArgumentException”
【发布时间】: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


    【解决方案1】:

    看起来您正在将 txtSeminar 设置为研讨会。 txtSeminar 似乎是一个 TextField,你应该使用 txtSeminar.text

    [new setValue:self.txtseminar forKey:@"seminar"];
    

    【讨论】:

    • 用于保存数据,谢谢 :) 但它没有传递给 TeacherDataViewController.m 并且代码崩溃
    • 不错。将此帖子标记为答案,以便我们关闭它。为代码崩溃问题发布另一个问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-20
    • 1970-01-01
    相关资源
    最近更新 更多