8

I have created a core data project in xcode 8.2, After that I created an entity say "TestEntity" in .xcdatamodeld

I am creating entity object by using following code as per given in some tutorials for swift3. enter link description here

    let appDele = (UIApplication.shared.delegate) as! AppDelegate
    let cont = appDele.persistentContainer.viewContext

    let ent  = TestEntity(context:cont)

But I got an error "use of unresolved identifier 'TestEntity'."

how to resolve that error. enter image description here enter image description here

Raj Aggrawal
  • 761
  • 1
  • 6
  • 21

8 Answers8

12

Product->Buld for testing solved this issue for me(it solves most unresolved identifier bugs for some strange reason)

JP Aquino
  • 3,946
  • 1
  • 23
  • 25
  • Oh my GOD. Thanks. – ChrisH May 21 '19 at 16:13
  • Building the project is apparently necessary to resolve the references between the files in the project. In any case this is a "soft" error, meaning it's only an error in the editor and not a "hard" build error. – tromgy Nov 25 '19 at 17:17
  • For others: there are times when you need create the NSManagedObjectSubclasses in order to override or write custom parts to the class. For situations where the declarations in the object are sufficient as is, you shouldn't need to. In such cases when this error occurs, JP Aquino's answer to Build For -> Test works and is probably the correct response to the problem. – Justin Ngan Jul 26 '20 at 08:29
12

I solved it by changing import Foundation to import UIKit.

Because UIApplication isn't included in Foundation framework.

Bright
  • 5,699
  • 2
  • 50
  • 72
  • This is correct. One weird thing though, I have two projects, one does not need to import the UIKit, and the other needs to. – Glenn Posadas May 27 '17 at 16:31
1

"use of unresolved identifier 'TestEntity'."Means that you are trying to use a class/struct that is declared nowhere. Possibly you need to generate the NSManagedObjects for your entities.

enter image description here

Giuseppe Lanza
  • 3,519
  • 1
  • 18
  • 40
  • 3
    Creating NSManagedObjectSubClass is not necessory in XCode 8.2 ans swift3. – Raj Aggrawal Jan 18 '17 at 06:03
  • Giuseppe Lanza have look into to core data in swift 3. Swift 3 has some changes for core-data. Don't go for old logic – Raj Aggrawal Jan 18 '17 at 06:46
  • http://stackoverflow.com/a/37905573/1754559 The default behavior is the previous one. – Giuseppe Lanza Jan 18 '17 at 09:50
  • I know this is the default behavior, but we need to move on to updated contents. In swift3 it is not required. And What you have told , I done that when I was learner in objective C and Xcode5. Before downvote you need to understand the problem.. – Raj Aggrawal Jan 18 '17 at 11:53
  • Did you try to do the same now? I am completely sure that generating the subclasses would solve your problem. Check the link I posted before. It is up to date. The default behavior is the previous one: You have to generate the subclasses manually. It's the only possible explanation Raj. Unresolved entity: The class is not defined anywhere. Downvote as much as you want, but that's it. – Giuseppe Lanza Jan 18 '17 at 11:56
  • I did the same and solve the problem but in swift 3 it not no longer needed so that why I am using that. I think you want to forcefully to use that. why don't you try the new things and gt updated. – Raj Aggrawal Jan 18 '17 at 12:08
  • You need to watch the WWDC video. [3]: https://developer.apple.com/videos/play/wwdc2016/242/ – Raj Aggrawal Jan 18 '17 at 12:19
  • Thanks a lot for your wise comments. and I am thanksful to your nice answer. But I asked some other. – Raj Aggrawal Jan 18 '17 at 12:25
  • You updated your question. You asked why you are having the issue. I told you why. The classes are not defined. Whatever you did to get what's new in core data didn't work and I also told you why: the default behaviour is the old one: you must create sub classes manually. Change your model behaviour to get things done automatically. – Giuseppe Lanza Jan 18 '17 at 12:28
1

Try the following things:

  1. Clean and Build the project after you've added the Entity to your CoreData file
  2. Delete the Entity and create it again - redo the step 1
  3. Close and reopen xcode - redo step 1 and 2
  4. Update xcode to newest version (worked for me) and redo step 1,2 and 3

I can't tell you what solved the issue. But I glad it works now!

Dani Lo
  • 11
  • 1
1

Closing Xcode and reopening it worked for me.

0

In my case it was because I had renamed the entity in the .xcdatamodeld file.

But renaming the entity in the side menu only applied to the Name property, and not also the Class Name. After also renaming the Class Name in the Data Model inspector, it worked.

Gandalf Saxe
  • 381
  • 3
  • 7
0

Just ran into this same problem, was using Xcode-generated Managed Object subclasses. Removed references from Xcode project and re-added them. Problem went away.

0

Change the import Foundation to import UIKit and check, if same problem occurs, Close the Xcode and re-open again it'll work

Praveen
  • 91
  • 2
  • 4
  • I think it's Xcode flaw, this happened many times for me. When you run and build your app bunch of times and added more swift files the Xcode cannot identify or find the identifier from the correct file. Like, sometimes the Xcode won't show autocompletions, the only solution is to "close and re-open" the Xcode. – Praveen Aug 23 '20 at 09:35