@NSManaged for Int16 or Int64 data types in CoreData

335 views Asked by At

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.

2

There are 2 answers

0
Ashley Mills On

For time and timestamp fields, you should probably be using a Date type.

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.

0
Tom Harrington On

Core Data is written in Objective-C and sometimes you really need to know that to use it. This is one of those times.

When you see @NSManaged, it means that normal Swift rules may not apply because Core Data is doing stuff that only works in Objective-C. Here it means that you can't use @NSManaged with optional primitive types (all numerics, booleans) because Objective-C doesn't have Swift-style optionals.

You have a couple of options.

  • You could make them non-optional. Core Data will give them a default value of 0 (or false for a boolean). You can provide a different default in your Core Data model if you like.
  • You could make the properties NSNumber, and then use NSNumber functions like intValue to get the integer value of the property.