Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 3 Core Data "Entity" error: `Use of undeclared type`

Use of undeclared type 'Transcription'

I'm following this simple tutorial of Core Data in Swift 3 (https://learnappdevelopment.com/uncategorized/how-to-use-core-data-in-ios-10-swift-3/)

and I get the above error on the line: let fetchRequest: NSFetchRequest<Transcription> = Transcription.fetchRequest()

I double checked and the Entity "Transcription" is spelled correctly in my .xcdatamodeld file

The tutorial was designed for Swift 3, but there was another change since it was released that I fixed, so I'm guessing some other change to Swift in the past 2 months has caused this error.

I'm brand new to Core Data, so I don't know how to debug this. I'd be very grateful for a solution!

like image 302
PlateReverb Avatar asked Oct 08 '16 16:10

PlateReverb


3 Answers

In my case, Xcode wasn't finding the automatically generated class, which is something like that:

//
//  Teste+CoreDataClass.swift
//  
//
//  Created by Laura Corssac on 2/2/20.
//
//  This file was automatically generated and should not be edited.
//

import Foundation
import CoreData

@objc(Teste)
public class Teste: NSManagedObject { }

After quitting Xcode and reopening it, my problem was solved.

like image 89
Laura Corssac Avatar answered Sep 21 '22 20:09

Laura Corssac


Dont forget import CoreData to your subclass

like image 15
Badre Avatar answered Nov 14 '22 18:11

Badre


This happened possibly as Xcode is unable to determine the path of the .xcdatamodel, a related error can be seen when cleaning the the project. This error happen to one of my project when changing the name of the .xcdatamodel.

Resolution involves the following:

  • go to Product -> Clean (Shift-Cmd-K)
  • if the error persists
    • Ctrl-click (right-click) the problematic .xcdatamodel -> Show in Finder
    • Drag the .xcdatamodel to import to the project, a new entry should be created
    • Remove the previous .xcdatamodel entry.
  • Build (Cmd-B) to confirm the fix
like image 12
Marco Leong Avatar answered Nov 14 '22 19:11

Marco Leong