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)

Cœur
  • 37,241
  • 25
  • 195
  • 267
Ninja
  • 309
  • 7
  • 26
  • 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. – Amin Negm-Awad Dec 18 '17 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 08 '18 at 17:22

1 Answers1

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

LastMove
  • 2,482
  • 1
  • 15
  • 25
  • 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 '17 at 16:54
  • Swift nil ~= Objective-C nil. As far as Int? in Obj-C, look at: https://stackoverflow.com/questions/24221407/can-a-swift-optional-int-int-be-exposed-to-objective-c-via-bridging – GetSwifty Dec 18 '17 at 18:10