Chandan kumar
Chandan kumar

Reputation: 1084

CoreData issue: -[NSManagedObject setValue:]: unrecognised selector sent to instance

I have Created the entity(Article) and able to fetch saved data. Now I want to show the data on view controller but on the third line, it is getting a crash("Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Article modiffied_date]: unrecognized selector sent to instance 0x60800009ccf0").

currentSelectedArticle is object of Article

 1. title = currentSelectedArticle?.title 
 2. image_url = currentSelectedArticle?.image_url 
 3. newdate = currentSelectedArticle?.modiffied_date

But When I used to print currentSelectedArticle.It's showing below result.

 <Article: 0x60800009ccf0> (entity: <null>; id: 0xd000000000080024 <x-coredata://D5402915-E542-4044-B14B-A3BF423847EC/Article/p2> ; data: <fault>)

What am I doing wrong?

Upvotes: 0

Views: 341

Answers (2)

Dave Weston
Dave Weston

Reputation: 6635

When you print the description of a managed object before it's been faulted in, you don't get a lot of info. I suggest you try the following code to debug:

if let article = currentSelectedArticle {
    title = article.title
    image_url = article.image_url
    print("Selected article: \(article)")
}

It seems like title and image_url are valid, so retrieving them will cause Core Data to fault in the object, then when you print you'll be able to see the values for all of its properties.

Upvotes: 1

Sudipto Roy
Sudipto Roy

Reputation: 6795

I faced the same problem while pulling data from CoreData ! So , I implemented it in Swift 3 like this ..

Step 1 : Add import CoreData

Step 2 : Add the code below . .

let context = ( UIApplication.shared.delegate as! AppDelegate ).persistentContainer.viewContext
var request = NSFetchRequest<NSFetchRequestResult>()
request = Article.fetchRequest()
request.returnsObjectsAsFaults = false
let arrayArticle = try context.fetch(request)

Upvotes: 1

Related Questions