Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift equivalent for @protocol(DelegateType)

I'm working with ReactiveCocoa in Swift. I need to use the following method:

rac_signalForSelector(selector: Selector, fromProtocol: Protocol?)

Passing the selector works fine with Selector("method:"), but I cannot find how to pass the delegate protocol to the fromProtocol parameter.

What is the proper way to pass the Protocol type from a delegate to a method signature like this?

EDIT: Adding method documentation and best attempt

The documentation for this method is as follows:

selector - The selector for whose invocations are to be observed. If it doesn’t exist, it will be implemented using information from protocol, and may accept non-object arguments and return a value. This cannot have C arrays or unions as arguments or return type.

protocol - The protocol in which selector is declared. This will be used for type information if the selector is not already implemented on the receiver. This must not be NULL, and selector must exist in this protocol.

I have tried sending in DelegateType.self, and I end up receiving this error:

Could not find an overload for rac_signalForSelector that accepts the supplied arguments

like image 688
Shane N Avatar asked Jul 07 '14 17:07

Shane N


People also ask

What is delegate protocol in Swift?

In Swift, declaring a delegate property is just like declaring any other property and you specify the protocol name as the type of the property. You may notice the question mark syntax which indicates that it's a property with an optional value (there may or may not be an object assigned to it).

What is the difference between delegate and protocol in Swift?

Protocol is a set of methods (either optional or required) that would be implemented by the class which conforms to that protocol. While, delegate is the reference to that class which conforms to that protocol and will adhere to implement methods defined in protocol.

How do you write a protocol in Objective C?

Protocols are implemented in the classes conforming to the protocol. A simple example would be a network URL handling class, it will have a protocol with methods like processCompleted delegate method that intimates the calling class once the network URL fetching operation is over. A syntax of protocol is shown below.

What is protocol in IOS Objective C?

Objective-C allows you to define protocols, which declare the methods expected to be used for a particular situation. This chapter describes the syntax to define a formal protocol, and explains how to mark a class interface as conforming to a protocol, which means that the class must implement the required methods.


Video Answer


1 Answers

Did you use @objc when you declared your protocol?

I believe SomeProtocol.self is right right way to pass it but since you're passing it into an obj-c API it needs to be prefixed with @objc like this example from the docs:

@objc protocol HasArea {
    var area: Double { get }
}

Edit: Turns out the protocol in question is from a library (written in objective-c, so already compatible with objective-c), not defined in Swift.

That being the case, it's probably a compiler bug, so before you do anything else make sure you're using the latest version of Xcode (beta 3 at the time of writing).

If that doesn't work out, I think Tommy's idea to use NSProtocolFromString is the best recourse until the compiler bug is fixed.

like image 72
Jiaaro Avatar answered Sep 28 '22 00:09

Jiaaro