Collection view and protocols - Protocols

8 important questions on Collection view and protocols - Protocols

What is the "problem" with id?

id is a pointer to unknown class.

Do you define methods in the protocol declaration?

No, it;s only the list of methods, not their implementation.

Where do @protocol declarations go?

In header files.
  • Higher grades + faster learning
  • Never study anything twice
  • 100% sure, 100% understanding
Discover Study Smart

How does a variable wich implements a protocol look like?

id <Foo> obj = [[MyClass alloc] init];

How and where can you "promise" to implement a protocol?

You can do this by typing the name in angle brackets <> i.e.:
@interface MyClass : NSObject <Foo>

it can also be done in the implementation file, that way its private. i.e.:

@interface MyClass() <Foo>
@end

@implementation MyClass

@end

Can you declare methods arguments to require a protocol

yes you can, for example:
-(void) giveMeFooObject: (id <Foo>) anObjectImplementingFoo;
or even a property:
@property (nonatomic, weak) id <Foo> myFooProperty;

Does the use of protocols make difference at runtime?

No, think of it as powerful documentation, "compiler sugar"

Where are protocols most used in iOS?

delegates and dataSources

The question on the page originate from the summary of the following study material:

  • A unique study and practice tool
  • Never study anything twice again
  • Get the grades you hope for
  • 100% sure, 100% understanding
Remember faster, study better. Scientifically proven.
Trustpilot Logo