Need a little assistance regarding @Persisted vs @objc dynamic

Am very green to Realm, and just installed it, as well as typed the tutorials demo to make sure it was working as needed.

However, when trying to define an Int for my db struct:

class MasterList : Object {
	@objc dynamo  var sheetNo 	: Int?

I get the following error:

Property cannot be marked @objc because its type cannot be represented in Objective-C

However, replacing the declaration with:
@Persisted var coll : Int?

Removes the error. I haven’t run all the code yet, so not sure if @Persisted will give me a headache further down. But figured I’d ask now, what the difference is in this case.

Thank you

That’s not really valid code so stick with @Persisted var coll : Int?

A bit of explanation may help; Realm has it’s roots in ObjC. Over time, Swift came into play and support for that developed. Initially, properties in Swift would be defined like this

@objc dynamic var someInt = 0 // non optional Int
let someOptionalInt = RealmProperty<Int?>() //optional Int

but that wasn’t very Swift like. As things progressed Realm was simplified and retrofitted to more align with Swift naming conventions and style. That change happend at Realm 10.10.

So from 10.10 forward properties look more Swift like

@Persisted var intName: Int //non-optional Int
@Persisted var optIntName: Int? //optional Int

You should be using @Persisted to define all Realm properties as shown in the Supported Types documentation.

1 Like

Thank you for your prompt reply. Makes me feel better using it, knowing that that’s how it should be.

Would still like to know why I couldn’t create the optional Int below using @objc dynamic. Just to know.

This doesn’t work because it’s mixing two different SDK formats for an optional Int.

Here are the options:


@Persisted var sheetNo: Int?

Swift Pre 10.10.0

let sheetNo = RealmProperty<Int?>()


@property NSNumber<RLMInt> *sheetNo;