在Swift中为Parse的PFRelation声明一个只读的@NSManaged属性

时间:2022-09-12 08:30:13

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:

我在我的iOS应用程序中使用Parse对象存储,我为Parse对象创建了一个自定义子类,看起来有点像这样:

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:

relatedThings属性有效:我可以从商店中获取相关对象。但是,我不断收到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.

在Objective-C中,我可以很容易地将该属性标记为只读,但我不确定如何在Swift中执行此操作以使警告静音。

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

使用let而不是var不允许与@NSManaged结合使用。

Adding private(set) has no effect either:

添加私有(set)也没有效果:

@NSManaged private(set) var relatedThings: PFRelation

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

那么Parse如何期望我宣布关系属性呢?

3 个解决方案

#1


3  

Now you should use:

现在你应该使用:

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

#2


2  

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.

Tom的回答对我不起作用,因为PFObject不是NSManagedObject的子类,因此它不实现willAccessValueForKey,primitiveValueForKey和didAccessValueForKey方法。

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

但是,使用PFObject自己的objectForKey方法是正确的等效解决方案:

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.

您仍然需要强制转换方法返回PFRelation的值,或者根据Tom的版本,将属性的类型转换为可选的。

#3


1  

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.

以下是使用Swift创建只读Core Data属性的方法。我不知道这是否是Parse正在寻找的东西,但它是如何在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.

虽然Xcode在属性上使用@NSManaged生成NSManagedObject子类,但实际上并不需要它。可以使用等效的Swift计算属性替换@NSManaged属性,这些属性使用Core Data基元访问器来获取/设置属性值。如果不包括setter,你就可以有效地阅读该属性。

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

这意味着用以下内容替换现有的relatedThings声明:

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.

这个获取代码的工作方式与使用@NSManaged时相同。但由于没有设置,该属性是只读的。

#1


3  

Now you should use:

现在你应该使用:

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

#2


2  

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.

Tom的回答对我不起作用,因为PFObject不是NSManagedObject的子类,因此它不实现willAccessValueForKey,primitiveValueForKey和didAccessValueForKey方法。

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

但是,使用PFObject自己的objectForKey方法是正确的等效解决方案:

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.

您仍然需要强制转换方法返回PFRelation的值,或者根据Tom的版本,将属性的类型转换为可选的。

#3


1  

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.

以下是使用Swift创建只读Core Data属性的方法。我不知道这是否是Parse正在寻找的东西,但它是如何在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.

虽然Xcode在属性上使用@NSManaged生成NSManagedObject子类,但实际上并不需要它。可以使用等效的Swift计算属性替换@NSManaged属性,这些属性使用Core Data基元访问器来获取/设置属性值。如果不包括setter,你就可以有效地阅读该属性。

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

这意味着用以下内容替换现有的relatedThings声明:

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.

这个获取代码的工作方式与使用@NSManaged时相同。但由于没有设置,该属性是只读的。