NSMutableArray Extension for Bidirectional Association Management

I am a big fan of code generation (from UML) and coming from the Java world, I am wondering how to implement automatic control of bidirectional associations in Objective-C.

An associate partner image Address, one-to-many and accessible from both ends. I would like to ensure that if I add an address to a partner, Address should automatically find out about his partner.

Thus, the implementation template must have NSMutableArray * on the partner side and partner * on the address side. The property on the address side is easy to implement, because setPartner: (Partner *) aPartner can automatically insert the address (itself) into the NSMutableArray address management partner. The other side, however, is not so easy to implement. The standard implementation template for many references in Objective-C appears to be NSMutableArray, accessible via the get @property method. The object owned by this NSMutableArray can then insert the Address object into an array, which, of course, will not update the other side.

I know that there are other templates for such association management, for example, using the addTo ... () and removeFrom ... () methods. But I still do not know if this will be consistent with other Cocoa programming principles or even not allow Cocoa to be used efficiently. Here I am thinking of Interface Builder. Not much experience, but I saw something called an ArrayController that seems pretty handy, but it also seems to be expected that a property like NSMutableArray will work. And if this guy inserts objects into an array, I need to intercept it and configure the other side.

Java, NSMutableArray , . ? , , , . ? . , . !

+3
1

Cocoa. . .

: Cocoa , . - , . NSArray , , . , , ( ) .

NSObject, "" NSArray (.. [ "field" ]), , .

, , , , "--", " " " ", CoreData. CoreData - , . , , - , , .

+9

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


All Articles