使用Coredata Swift发送到实例的无法识别的选择器 [英] unrecognized selector sent to instance with Coredata Swift

查看:143
本文介绍了使用Coredata Swift发送到实例的无法识别的选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我尝试更新核心数据模型的值时,我都会收到此错误。
这是我的模特

I keep getting this error whenever I try to update a value of core data model. Here is my model

import Foundation
import CoreData

@objc(Habit)
class Habit: NSManagedObject {

    @NSManaged var name: String
    @NSManaged var trackingType: NSNumber
}

这是我的代码tableViewCell

Here is my code tableViewCell

override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        if selected {
            self.accessoryType = UITableViewCellAccessoryType.Checkmark
            if self.habit? != nil {
                self.habit?.trackingType = index

            }

        } else {
            self.accessoryType = UITableViewCellAccessoryType.None
        }
        // Configure the view for the selected state

    }

我一直收到错误因未捕获的异常而终止应用程序'NSInvalidArgumentException',原因:' - [习惯setTrackingType:]:无法识别的选择器发送到实例0x7fdcbb002c90'


self.habit?.trackingType = index

at line self.habit?.trackingType = index

我很难在过去两天修复这个问题。

I am struggling to fix this for last 2 days.

编辑:

模型习惯以下面的方式初始化

The model habit is initialized in below way

func getHabits() -> [AnyObject]{
        let entityDescription =
        NSEntityDescription.entityForName("Habit",
            inManagedObjectContext: managedObjectContext!)

        let request = NSFetchRequest()
        request.entity = entityDescription
//        
//        let pred = NSPredicate(format: "(trackingType != -1)")
//        request.predicate = pred

        var error: NSError?

        var objects = managedObjectContext?.executeFetchRequest(request,
            error: &error)

        return objects!;
    }

返回的列表在应用程序的各处使用。基本上我从列表中获取和项目并更新其属性然后再次保存

The returned list is used everywhere in the app. Basically I fetch and item from the list and update its attributes and then save it again

推荐答案

好的,所以你得到的原因是错误是肯定的,因为 self.habit 引用的对象不是 Habit 对象。找出对象真正原因的最简单方法是调用:

Ok so the reason you are getting the error is most certainly because the object referenced by self.habit is not a Habit object. The easiest way to find out what the object really is is to call:

print(NSStringFromClass(habit.class))

使用核心数据和自定义 NSManagedObjects ,您需要制作确保实体:'Habit'(在您的数据模型中)具有设置为Habit的类。这可确保Core Data将带有Habit实体描述的已获取对象强制转换为Habit类。如果你不这样做,那么 getHabits func将返回一个 NSManagedObject 的数组,而不是<$的数组c $ c>习惯 s。如果是这种情况,那么代码: println(NSStringFromClass(habit.class))将打印NSManagedObject调试器。

With core data and custom NSManagedObjects you need to make sure that the entity: 'Habit' (in your data model) has a class set to Habit. This makes sure that Core Data casts your fetched objects with an entity description of 'Habit' to the Habit class. If you are not doing this then the getHabits func will be returning an array of NSManagedObjects not an array of Habits.If this is the case then the code: println(NSStringFromClass(habit.class)) will print "NSManagedObject" to the debugger.

作为附注,当您从Core Data数据库中获取对象时,确实需要检查错误。添加以下行:

As a side note, you really need to check for errors when you fetch objects from a Core Data database. Add the lines:

if objects? == nil {
    print("An error occurred \error.localisedDescription")
}

如果有任何错误,请原谅我的swift,我通常使用Objective-C。

Please forgive my swift if there are any errors, I normally use Objective-C.

编辑:为了纠正失败在NSManagedObject类'X'错误上调用指定的初始值设定项。如果未正确实例化 NSManagedObject ,则会触发此错误。你不能调用 [[MyManagedObject alloc] init]; 你必须调用 initWithEntity:insertIntoManagedObjectContext 而不是:

In order to correct Failed to call designated initializer on NSManagedObject class 'X' error. This error is fired when you do not correctly instantiate an NSManagedObject. You must not call [[MyManagedObject alloc] init]; you have to call initWithEntity:insertIntoManagedObjectContext instead:

MyManagedObject *obj = [[MyManagedObject alloc] initWithEntity:[NSEntityDescription entityForName:@"MyManagedObject" inManagedObjectContext:context] insertIntoManagedObjectContext:context];

如果你不想要对象 obj 要插入上下文,您可以通过 nil 上下文参数。但是,如果您想要撤消管理并且能够将对象保存到数据库,则需要将其与上下文相关联。

If you do not want the object obj to be inserted into a context you can pass through a nil context argument. However, if you want undo management and the ability to save the object to the database it needs to be associated with a context.

如果您想要自定义初始化然后你可以覆盖 awakeFromInsert awakeFromFetch 方法/函数。

If you want to have a custom initialisation of an object then you can override the awakeFromInsert and awakeFromFetch methods/functions.

这篇关于使用Coredata Swift发送到实例的无法识别的选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆