Communication between objects - transferring itself to another object / Objective-C, Cocoa

Say we have the first Object 'Controller', and it initializes another object named "Tasks" when passing "self" for reference. Object Tasks can now exchange messages and send messages to the supercontroller object. Is this correct for communication between objects? Is this usually done? I just started to really learn programming, so I wonder if everything is in order. Until that moment, I relied on delegates and notifications. Is this a good practice?

Example:

// Controller Object
task = [[Task alloc] initWithController: self];
- (void) runMethod: (NSString *) incoming {
NSLog(@"%@", incoming);
}

// Task Object
- (id) initWithController: (Controller *) ctrlr {
controllerPointer = ctrlr;
[controllerPointer runMethod:@"hello"];
return self
}


// All this should print out "hello"

And are there other ways of communicating, interacting between objects?

+3
source share
3 answers

(Controller ) (Task). Model-View-Controller (MVC), , , , , , , , , .

. , , , ? Task ? , , .

Cocoa , : (KVO). . , NSNotificationCenter. , .

KVO . , Cocoa , . (KVO Cocoa Bindings, , , iPhone SDK)

: , - . , .

, , , , .

+7

?

. .

GC, , , Task , , , , Controller Task .

: controllerPointer . Controller.

, ?

, , , , . , Task , .

, , , Controller , Task.

[[Controller sharedInstance] runMethod:@"hello"];

, , .

+2

, , , , . 100% .

#import <Foundation/Foundation.h>


@protocol MMNotificationDelegate <NSObject>
- (void)acquireForwardClass:(id)f_class;
- (void)notifyForwardClass:(NSDictionary*)dict;
@end

, MMNotificationDelegate, , , , "" .

+1

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


All Articles