Declare a read-only @NSManaged property in Swift for Parse's PFRelation

1.4k Views Asked by At

I'm using Parse object store in my iOS application and I've created a custom subclass for my Parse object, which looks somewhat like this:

class MyThing: PFObject, PFSubclassing {

   // ...PFSubclassing protocol...

   @NSManaged var name: String
   @NSManaged var somethingElse: String
   @NSManaged var relatedThings: PFRelation

 }

The relatedThings property works: I am able to fetch the related objects from the store. However, I keep getting this warning from Parse:

[Warning]: PFRelation properties are always readonly,
but MyApp.MyThing.relatedThings was declared otherwise.

In Objective-C, I could have easily marked that property as readonly, but I am unsure how to do that in Swift to silence the warning.

Using let instead of var is not allowed in combination with @NSManaged.

Adding private(set) has no effect either:

@NSManaged private(set) var relatedThings: PFRelation

So how does Parse expect me to declare the relationship property?

3

There are 3 best solutions below

0
On BEST ANSWER

Now you should use:

var relatedThings: PFRelation! {
    return relationForKey("relatedThings")
}
2
On

Here's how you'd make a read-only Core Data property with Swift. I don't know if this is what Parse is looking for, but it's how things would work in Swift.

Although Xcode generates NSManagedObject subclasses with @NSManaged on the attributes, it's not actually required. It's fine to replace @NSManaged attributes with equivalent Swift computed properties that use Core Data primitive accessors to get/set property values. You'd make the property effectively readonly by not including a setter.

That would mean replacing your existing relatedThings declaration with something like this:

var relatedThings : PFRelation? {
    get {
        self.willAccessValueForKey("relatedThings")
        let things = self.primitiveValueForKey("relatedThings") as? PFRelation
        self.didAccessValueForKey("relatedThings")

        return things
    }
}

This get code will work the same as it would if you were using @NSManaged. But by not having a set, the property is read-only.

1
On

Tom's answer did not work for me, as PFObject is not a subclass of NSManagedObject, thus it doesn't implement the willAccessValueForKey, primitiveValueForKey, and didAccessValueForKey methods.

However, using PFObject's own objectForKey method is the correct equivalent solution:

var relatedThings: PFRelation! {
    return objectForKey("relatedThings") as? PFRelation
}

You still need to either force-cast the value that method returns to PFRelation, or, as per Tom's version, turn the property's type into an optional.