I am trying to build a simple tracker in SwiftUI as a learning experience and I have found a couple tutorials for handling CoreData in SwiftUI. Trying to follow them has become quite a pain in the ... The first one I am trying to follow is Here There is a part that shows something like this...
public class BlogIdea: NSManagedObject, Identifiable {
@NSManaged public var ideaTitle: String?
@NSManaged public var ideaDescription: String?
}
extension BlogIdea {
// ❇️ The @FetchRequest property wrapper in the ContentView will call this function
static func allIdeasFetchRequest() -> NSFetchRequest<BlogIdea> {
let request: NSFetchRequest<BlogIdea> = BlogIdea.fetchRequest() as! NSFetchRequest<BlogIdea>
// ❇️ The @FetchRequest property wrapper in the ContentView requires a sort descriptor
request.sortDescriptors = [NSSortDescriptor(key: "ideaTitle", ascending: true)]
return request
}
}
The problem I am running into is my data is a String, and Int16, and an Int64. However whenever I try to use those I get an error in xcode saying
Property cannot be marked @NSManaged because its type cannot be represented in Objective-C
I have tried marking it as Double, Int16, and Int64 in my CoreData model and all of them produce the same error in xcode.
My class is simple...
class TrackedData: NSManagedObject, Identifiable {
@NSManaged public var dateString: String?
@NSManaged public var time: Int16?
@NSManaged public var timestamp: Int64?
}
Then I have an extension that will help me filter the results. From what I can see in the core data model you only have a few options for an Int type data (Int16, Int32, Int64, Double, Float, and Decimal), but I have yet to be able to figure out how I can actually use any of those with the @NSManaged annotation. Is there some special way of dealing with Int type data in CoreData that I am missing? Sorry I am new to SwiftUI and dealing with the @FetchRequest stuff so I don't see what I am missing.
Thank you.
For
timeandtimestampfields, you should probably be using aDatetype.If you use
Int16,Int63, etc, it has to be non-optional.If you allow Xcode to generate the class files for you, you'll see the correct types.