zono
zono

Reputation: 8584

CoreData: Not null property should be optional?

I'm not sure about best practice of Swift and CoreData. Do you think that not null property should be optional basically?

for example

import Foundation
import CoreData

class Item: NSManagedObject {
    @NSManaged var itemId: String // Not null property
    @NSManaged var itemPrice: String? // null is possible
}

I wonder that I should notify to other programmers which property is not-null or not.

I would like to know what do you do usually. This question sound an opinion-based but I'm sure how dealing the optional is helpful for others.

FYI I found similar question

CoreData - Setting a property of entity to be not null - Should the attribute be set as Optional or Mandatory

Upvotes: 1

Views: 1404

Answers (1)

Shobhakar Tiwari
Shobhakar Tiwari

Reputation: 7892

From https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CoreData/Articles/cdMOM.html#//apple_ref/doc/uid/TP40002328-SW6 .. it is clearly mentioned - You can specify that an attribute is optional—that is, it is not required to have a value. In general, however, you are discouraged from doing so—especially for numeric values (typically you can get better results using a mandatory attribute with a default value—in the model—of 0)

Upvotes: 6

Related Questions