2

I have a Objective C class that has methods that look like this:

@class Client;

@protocol ClientDelegate <NSObject>

@optional
-(void) receivedMessageFromClient : (id) message;

@end

@interface Client : NSObject 

+(id) setup; 
 @property(nonatomic, strong) id <ClientDelegate> clientDelegate;

// more method declarations below

I am implementing the ClientDelegate in my Swift class like this:

class HomeViewController: UIViewController, ClientDelegate {
    var client : AnyObject?
    var delegate: ClientDelegate?

    override func viewDidLoad() {
        super.viewDidLoad()

        client = Client.setup() as! Client
         client.clientDelegate = self
    }

func receivedMessageFromClient(message: AnyObject) {
        print("Message recieved: \(message)")
    }
}

This is giving me a compiling error:

Cannot assign to property: 'self' is immutable

When I remove the lines

client = Client.setup() as! Client
client.clientDelegate = self

the code compiles and calls the method in the Client class that in turns sends a message to receivedMessageFromClient, but the method is not called in HomeViewController. It seems that everything is setup with the exception of assigning self to be the delegate.

Ashish Agarwal
  • 14,555
  • 31
  • 86
  • 125
  • Delegates usually have `weak` attribute instead of `strong`. And RTFM about protocols https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Protocols.html – Alexmelyon Nov 20 '15 at 08:24
  • Possible duplicate of [I am using objective-c classes in swift2 , how i can implement delegate methods and bind that delegate](http://stackoverflow.com/questions/33774912/i-am-using-objective-c-classes-in-swift2-how-i-can-implement-delegate-methods) – Janak LN Nov 20 '15 at 08:29
  • do you have a bridging header ? – Bobj-C Nov 20 '15 at 09:09
  • yes, and it is working. I am able to call and use methods of the class – Ashish Agarwal Nov 20 '15 at 09:12
  • Reading your code, I think you need to revise the delegation pattern [check this out](http://www.alexefish.com/post/522641eb31fa2a0015000002). You first need to sort out your delegate in objective c then implement it in Swift. – Rob Sanders Nov 20 '15 at 09:16

2 Answers2

11

Check out my comments on your question but this is a basic example of an Objective-C delegate implemented in Swift:

Objective-C:

@protocol MyDelegate <NSObject>

@optional
- (void)canImplementThis:(int)aVar;
@required
- (BOOL)needToImplementThis;

@end

@interface MyClass : NSObject

@property (nonatomic, weak) id<MyDelegate> delegate;

@end

Swift:

class SwiftClass : UIViewController, MyDelegate {

    var myObj : MyClass?

    override func viewDidLoad() {
        super.viewDidLoad()

        myObj = MyClass()
        myObj?.delegate = self
    }

    // MARK: - <MyDelegate>

    func canImplementThis(aVar : Int) {
        print("Called: \(aVar))
    }

    func needToImplementThis() -> Bool {
        return false
    }

}

Forgive any typos I typed this out straight into SO.

Mike Lischke
  • 48,925
  • 16
  • 119
  • 181
Rob Sanders
  • 5,197
  • 3
  • 31
  • 58
0

Had to change Int to Int32 for some reason so it fix the invalid selector issue, with Int32 on the swift side but then worked

Goose
  • 1