0

Today I began to create simple xcdatamodel with only 1 entity and few attributes in it. So when I created number attribute of type integer 16 in class definition of nsmanagedobject its type represented as NSNumber?

class JournalEntry : NSManagedObject {

    @NSManaged var date: Date?
    @NSManaged var height: String?
    @NSManaged var period: String?
    @NSManaged var wind: String?
    @NSManaged var location: String?
    @NSManaged var rating: NSNumber?
}

why not in int16? (I've read that objc doesn't have int? type) (because in model i allowed attribute to be optional) so if I won't assign value to NSNumber? property how it represented in objc during runtime? (it will be zero? p.s. I removed initial value as zero so there is no initial value)

2
  • Objective-C int (C int, because they are imported from C) does not have a nil value, because they are not objects. Usually one uses a special value to represent int. 0 is a bad idea in that case. This is called the zero is null antipattern. Dec 18, 2017 at 18:23
  • Where did that definition of JournalEntry come from? Did Xcode generate it? Did you use some other tool to generate it? Did you write it by hand? I tried to reproduce your problem in Xcode 10.1, but I could not find a way to make it put the properties inside the class. I could only make it put the properties inside an extension.
    – rob mayoff
    Dec 8, 2018 at 17:22

1 Answer 1

0

Since your rating is an optional NSNumber the initial value is nil (You can understand it as "no value") or the default value you provided in the CoreData Model editor. Also In swift you should be able to use Int64 instead of NSNumber

2
  • thank you please do you know how swift nil object represents in objc runtime? i mean if int? is nil so what it is would be in objc run time in what substance it will be converted
    – Ninja
    Dec 18, 2017 at 16:54
  • Swift nil ~= Objective-C nil. As far as Int? in Obj-C, look at: stackoverflow.com/questions/24221407/…
    – GetSwifty
    Dec 18, 2017 at 18:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.