Communication between objects in Objective C and Swift compared with Actionscript – part 3


Rather than passing the method itself to be called back, another callback strategy is to pass in an object that contains the callback methods. Delegation is such a design pattern, another way that children in Objective C and Swift can communicate with their parent. Delegates formalise the list of messages/methods that a child may callback in something called a protocol. A parent notifies the child what is the child’s ‘delegate’(usually the parent just defines itself as the delegate) and then implements the relevant available methods in the protocol. The delegate will then receive these callbacks from the child.


Delegates aren’t a popular structure in ActionScript but certainly could be followed using Interfaces as protocols. The analogy would be that a child defines a callback class with an interface(the protocol), and then the parent implements that interface and passes itself to the child. In fact the client property in NetStream is a type of delegate, the only difference being that its Interface(protocol) is implicit.

Objective C

Here’s a basic example of how to implement delegates and protocols in Objective C.
The child, let’s call it MyClass:

/// MyClass Header(.h) File
@class MyClass; 
	@protocol MyClassDelegate <NSObject>  
	    - (void) myClassDelegateMethod: (MyClass *) sender; 
	@end //end protocol

	@interface MyClass : NSObject {
	@property (weak, nonatomic) id <MyClassDelegate> delegate;


/// MyClass (.m) File
#import "MyClass.h"
@implementation MyClass 
	@synthesize delegate; 

	- (void) doSomething { //this method gets called by MyClass at some point
		//does some stuff here
	    [self.delegate myClassDelegateMethod:self]; //calls the MyClassDelegate method to notify the delegate

Now in the parent class, for example a ViewController called MYViewController

#import "MyClass.h"
@interface MYViewController:UIViewController <MyClassDelegate> { //make it a delegate for MyClassDelegate


myClass.delegate = self;          //set its delegate to self somewhere

//implement delegate method
- (void) myClassDelegateMethod: (MyClass *) sender {
    NSLog(@"MyClass called this method via MyClassDelegate!");


And here is the same example in Swift – much less verbose without header files.
First the child, MyClass:

protocol MyClassDelegate {
  func myClassDelegateMethod(sender:MyClass)
class MyClass {
	var delegate: MyClassDelegate?
	func doSomething {

Now the parent class, a ViewController we’ve called MYViewController

class MyViewController:UIViewController, MyClassDelegate {
	var myClass:MyClass
	init() {
		myClass = MyClass()
		myClass.delegate = self
	func myClassDelegateMethod(sender:MyClass) {
		println("MyClass called this method via MyClassDelegate!")

iOS development with Swift - book: video course:

Tagged with:
Posted in Flash, Objective C, Swift
4 comments on “Communication between objects in Objective C and Swift compared with Actionscript – part 3
  1. Tamas Darvas says:

    hi, in the swift implementation of MyViewController you are missing : myClass.delegate = self;

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: