29

I am trying to use an Optional Int in Realm and am getting an old error I think.

Code

dynamic var reps: Int? = nil

Error

'Property cannot be marked dynamic because its type cannot be represented in Objective-C'

I am using Realm 0.96.1 with XCode 7.1

I understand in the Realm documentation it says the Int isn't supported as an Optional but https://twitter.com/realm/status/656621989583548416. That is from the Realm twitter so thats why I am confused. Are Optional Int supported or still no?

Cody Weaver
  • 4,756
  • 11
  • 33
  • 51

2 Answers2

51

From the Realm docs:

String, NSDate, and NSData properties can be declared as optional or non-optional using the standard Swift syntax.

Optional numeric types are declared using RealmOptional:

class Person: Object {
    // Optional string property, defaulting to nil
    dynamic var name: String? = nil

    // Optional int property, defaulting to nil
    // RealmOptional properties should always be declared with `let`,
    // as assigning to them directly will not work as desired
    let age = RealmOptional<Int>()
}

let realm = try! Realm()
try! realm.write() {
    var person = realm.create(Person.self, value: ["Jane", 27])
    // Reading from or modifying a `RealmOptional` is done via the `value` property
    person.age.value = 28
}

RealmOptional supports Int, Float, Double, Bool, and all of the sized versions of Int (Int8, Int16, Int32, Int64).

UPDATE:

The Optional Ints that were mentioned in the Tweet by Realm were just regarding a bugfix for the RealmOptional way of implementing an Optional numeric value with the sized versions of Int

According to the guys from Realm you still have to use RealmOptional if you want to have Optional numeric values in a Realm object. You cannot simply use it like other Optional types.

So dynamic var reps: Int? will not work.

joern
  • 27,354
  • 7
  • 90
  • 105
  • I saw that in the docs just I thought in 0.96.1 Int was supported as well. I think those docs are for 0.96 – Cody Weaver Oct 26 '15 at 22:50
  • I just updated to RealmSwift 0.96.2 and it still gets the same error message that you describe when you try to define an Int as Optional. In release notes for 0.96.1 it says "Fix crash when using optional Int16/Int32/Int64 properties in Swift.". They only call it a bug fix, not a new feature. IMHO adding "real" Optional Int would be a new feature. So I guess you still have to use the `RealmOptional` – joern Oct 26 '15 at 23:10
  • I just got the confirmation from the Realm guys that 'RealmOptional` still has to be used for Optional numeric types: https://twitter.com/realm/status/658795663254253568 – joern Oct 27 '15 at 00:27
  • Has anybody have an explanation why Realm had to introduce their own type unable to plug into the optionals of primitive? – Paweł Brewczynski Apr 13 '17 at 15:22
  • why dot you declare as let? how do you change its value? – JBarros35 Aug 26 '20 at 10:54
  • @JBarros35 The explanation is right there in the code comments ;-) I took it from the Realm docs. – joern Aug 26 '20 at 11:24
1

In the case objective c, we can use optional like this

Optional numbers can be stored using an NSNumber * property which is tagged with the type of the number.

You can use NSNumber <RLMInt> *, NSNumber<RLMBool> *, NSNumber<RLMFloat> *, and NSNumber<RLMDouble> *.

Please just refer the example code

@interface OptionalTypes : RLMObject
@property NSString *optionalString;
@property NSString *requiredString;
@property NSData *optionalData;
@property NSDate *optionalDate;
@property NSNumber<RLMInt> *optionalInt;
@property NSNumber<RLMBool> *optionalBool;
@property NSNumber<RLMFloat> *optionalFloat;
@property NSNumber<RLMDouble> *optionalDouble;
@end
@implementation OptionalTypes
+ (NSArray *)requiredProperties {
    return @[@"requiredString"];
}
@end

For more info you can also check this link: https://realm.io/blog/realm-objc-swift-0-96-0/

IKKA
  • 6,297
  • 7
  • 50
  • 88