1

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

Community
  • 1
  • 1
zono
  • 8,366
  • 21
  • 75
  • 113
  • 2
    possible duplicate of [Swift + CoreData: Cannot Automatically Set Optional Attribute On Generated NSManagedObject Subclass](http://stackoverflow.com/questions/25485273/swift-coredata-cannot-automatically-set-optional-attribute-on-generated-nsman) – Stuart Mar 17 '15 at 07:34

1 Answers1

6

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)

Shobhakar Tiwari
  • 7,862
  • 4
  • 36
  • 71
  • Thank you so much. All right, I will set optional for null-possible properties in general and I will set default value if I need it. – zono Mar 17 '15 at 07:40
  • @zono if answer is suitable then make it accepted answer so that it can be useful for other . – Shobhakar Tiwari Mar 17 '15 at 07:42