Basic data. How to check if an object link exists without an error

There is object A and object B. Object B has one attribute, which is a transformable type (image), and one relation, which refers to object A. Object A may relate to one and only one, object B, or it may not be .

As I enumerate an array through my object, I want to check whether each object A has an object B. But I do not want to throw an error for object B (which will refer to the reverse imageToData NSValueTransformer). I just want to know if he is there or not. How can I do this without injecting object B into memory?

+6
source share
2 answers

I think you can just check

if (objectA.relationshipToB != nil) ... 

This will not cause an error for the associated object B , because you are not getting access to its properties.

+7
source

In Swift, I got

Could not find overload for '! = ', which accepts the provided arguments

error. My relationship was correctly marked as optional, but in the generated NSManagedObject my @NSManaged property was not ? after him. So this check with nil failed. Did I add ? and then I was able to check for a relationship.

Before (not working)

 @NSManaged var myRelationShip: MyClass 

After (works)

 @NSManaged var myRelationShip: MyClass? // <--- Added `?` 
0
source

Source: https://habr.com/ru/post/958198/


All Articles