Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type constraint on AnyObject or Any (AnyObject or Any that conforms to a protocol)

Tags:

swift

I'm trying to convert my Objective-C code to swift. In Objective-C I have the following protocol:

@protocol RWOverlaySelectionDelegate <NSObject>
    -(void)areaSelected:(UIView *)view allPoints:(NSArray *)points;
@end

and following class has a weak property that reference the protocol (obviously it's defined as weak to prevent strong reference cycle).

@interface RWMapSelectionLayer : UIView
    @property(weak, nonatomic) id <RWOverlaySelectionDelegate> delegate;
@end

Now the Swift equivalent:

the protocol:

protocol RWOverlaySelectionDelegate {
    func areaSelected(view:UIView,points:CGPoint[])
}

and the class that has a property which conforms to that protocol:

class RWMapSelectionLayer:NSObject {
    weak var delegate:RWOverlaySelectionDelegate?
}

But I'm getting 'weak' cannot be applied to non-class type 'RWOverlaySelectionDelegate' compile time error in this line : weak var delegate:RWOverlaySelectionDelegate?

Then I tried to convert my property to an AnyObject that conforms to RWOverlaySelectionDelegate using following syntax:

weak var delegate: AnyObject<RWOverlaySelectionDelegate>?

Now I'm interfering with Generics and compiler showing: Cannot specialize non-generic type 'AnyObject' error.

In another unsuccessful try I changed it to

weak var delegate: AnyObject:RWOverlaySelectionDelegate?

read it as "Delegate is on type AnyObject where AnyObject should conform to RWOverlaySelectionDelegate"

This is again incorrect because of two colons(:) in a single statement.

Would appreciate if someone can help me to enforce conformance to a protocol on AnyObject or Any

like image 633
Amir Rezvani Avatar asked Jun 05 '14 17:06

Amir Rezvani


People also ask

When to use AnyObject in Swift?

AnyObject can also be used as the concrete type for an instance of a type that bridges to an Objective-C class. Many value types in Swift bridge to Objective-C counterparts, like String and Int . The flexible behavior of the AnyObject protocol is similar to Objective-C's id type.

What's the difference between any and AnyObject?

AnyObject is only for reference types (classes), Any is for both value and reference types. So you should go for [String: Any] . Swift provides two special types for working with nonspecific types: Any can represent an instance of any type at all, including function types.

What is a protocol in Swift?

In Swift, a protocol defines a blueprint of methods or properties that can then be adopted by classes (or any other types). We use the protocol keyword to define a protocol. For example, protocol Greet { // blueprint of a property var name: String { get } // blueprint of a method func message() }


2 Answers

I have since found a better way of doing this in this answer.

Updated for recent Swift versions

As I mentioned in my comment on CjCoax's answer, prefixing the protocol with @objc prevents passing Swift object types (such as enums and structs) to delegate methods.

However, using : AnyObject will allow this behaviour while allowing the protocol to be used in a weak variable, though this method is not without its limitations. You can only make classes conform to any protocol that is marked with : AnyObject. I believe that this is a better trade-off than @objc provides.

protocol MyProtocol: AnyObject {
    ...
}
like image 197
Steve Wilford Avatar answered Oct 06 '22 00:10

Steve Wilford


I believe the recommended solution would be a class-only protocol. From the Swift Programming Language protocol page:

You can limit protocol adoption to class types (and not structures or enumerations) by adding the AnyObject protocol to a protocol’s inheritance list.

So in your example, your protocol definition would look like:

protocol RWOverlaySelectionDelegate: AnyObject {
    func areaSelected(view: UIView, points: NSArray)
}

With the addition of the AnyObject keyword, the compiler should no longer complain.

like image 24
chris838 Avatar answered Oct 06 '22 00:10

chris838