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.
weakattribute instead ofstrong. And RTFM about protocols developer.apple.com/library/ios/documentation/Swift/Conceptual/…