Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
507 views
in Technique[技术] by (71.8m points)

ios - unrecognized selector sent to instance with 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
}

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

    }

I keep getting error "Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Habit setTrackingType:]: unrecognized selector sent to instance 0x7fdcbb002c90'"

at line self.habit?.trackingType = index

I am struggling to fix this for last 2 days.

Edit:

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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))

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.

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")
}

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

EDIT: 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];

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.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...