Specify protocol of object in method parameters

mashers

I want to pass an object to a method which should conform to a protocol, and be able to call the methods which are defined that protocol. However, I can't find any way of doing so. For example:

+ (void)prepareToBounceInView:(UIView*<PixelUI>)pixelUIView 
    fromEdge:(PixelUIViewBounceEdge)edge;

The first parameter of the method is a UIView subclass which should conform to the PixelUI protocol, and the method should then be able to access properties and call methods defined in the protocol.

The only thing that worked was using id<PixelUI> as the type of the object, but then I can't access any of the properties or methods of the object itself, only those defined in the protocol; so I have to re-cast the object as (UIView*) when I need to treat it as the object it is, rather than as the delegate. This creates some ugly syntax, like this:

UIView *view = (UIView*)pixelUIView;
view.frame = pixelUIView.bounceBackFrame;

where view and pixelUIView are actually the same object, but have to be accessed using two different variable names.

If this is impossible then I will do as I have been doing and pass it in as id and then re-cast it. But if there is some syntax which will allow me to pass it in as its actual object while still specifying its protocol I'd love to know.

clemens

The order of the protocol and the star is important:

+ (void)prepareToBounceInView:(UIView<PixelUI> *)pixelUIView 
    fromEdge:(PixelUIViewBounceEdge)edge;

With this no casts should be necessary.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Is protocol conformance in method parameters optional?

Specify jackson serialization as an object method

Should I mark all method parameters as final and specify type

Specify mocked method's return value entirely regardless of parameters

How can I specify optional request parameters for a Grails command object?

How to specify position of an aframe object using angular parameters?

Specify a class that implements a protocol

Using generics with custom object in method parameters

Why not use Object for all method parameters?

Is there a way to combine parameters to the HttpGet method into an object?

Pass new object with the use of setter in method parameters

Is it possible to pass both an object and additional parameters with a method?

Object graph or individual properties as method parameters?

MOQ return new object from method with parameters

Error: The animation object does not specify a save_animation method

How to specify argument type of object method by parent key

Specify a settable property/variable in a protocol

Specify key exchange protocol with lftp

Specify List of Parameters in Keras

Specify parameters for menu functions

specify parameters in odoo module

Specify parameters for launcher program

How to specify parameters for input

How can I specify default values for method parameters in c#7 tuples?

How to specify a test with method name, class name and parameters in testng.xml?

How can I use randomisation to specify different object parameters to a single iteration in a loop?

Is mutating object-parameters in a method(in Java) a bad practice?

How to filter an object using multiple parameters in the specification method?

How to call a method with parameters from an optional object in java