"Int?" isn't covered by "@objc"?

I'm trying out an NSObject sub-class. I'm marking stored instance properties as "@objc dynamic," and it isn't working for "Int?" properties. (It does work for plain "Int" properties.) What am I supposed to use, "NSNumber?"?

You cannot use Optional directly, because there are no swift-style enums in Obj-C.
You cannot make it a nullable class pointer, because Int is not a class

Yes. Some apis use NSNotFound (aka Int.max) instead of nil, but it's not a good solution. NSNumber is much better at conveying that it's nullable

1 Like

Is this for Realm? I usually use a sentinel default value, so not an optional. I think Realm also has some kind of RealmOptional that you can use.

It's for mapping an attribute of an XML tag. The attribute may be implied (i.e. left out).

So not Realm. OK. I'd still probably go with the default value and call it a day. That almost always works for me.

The error says something like Objective-C cannot represent an optional.

You didn't say why you need to use @objc dynamic. Is this for interoperability with some Objective-C code?

FWIW, NSNumber uses tagged pointers, so it’s not as heavyweight as it appears (or as it used to be).

So if the boxed integer value doesn’t actually require 64 bits to store, it won’t need a heap allocation. That should cover the vast majority of numbers you’re likely to find in an XML document.

EDIT: Ah, it just occurred to me that this might only be true on Darwin platforms, but if you’re using Objective-C, I take it that isn’t a problem

1 Like

It’s for KVC/KVO use within a NSDocument sub-class.