Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle Objective-C protocols that contain properties?

People also ask

Can Objective-C protocols have properties?

You can have properties in a protocol, provided every class that conforms to your protocol have a corresponding @synthesize for that property, or provide a getter and setter.

How do you conform a protocol in Objective-C?

Objective-C Language Protocols Conforming to Protocols It is also possible for a class to conform to multiple protocols, by separating them with comma. Like when conforming to a single protocol, the class must implement each required method of each protocols, and each optional method you choose to implement.

What are Objective-C properties?

The Objective-C declared properties feature provides a simple way to declare and implement an object's accessor methods.

What are the properties protocols?

It aims to ensure that neighbours exchange sufficient information in a timely manner to minimise the scope for disputes between them; and to enable any such disputes to be readily resolved, including by alternative disputes resolution (ADR), keeping costs to a minimum.


The protocol is just telling everyone that knows about your class through the protocol, that the property anObject will be there. Protocols are not real, they have no variables or methods themselves - they only describe a specific set of attributes that is true about your class so that objects holding references to them can use them in specific ways.

That means in your class that conforms to your protocol, you have to do everything to make sure anObject works.

@property and @synthesize are at heart two mechanisms that generate code for you. @property is just saying there will be a getter (and/or setter) method for that property name. These days @property alone is enough to also have methods and a storage variable created for you by the system (you used to have to add @sythesize). But you have to have something to access and store the variable.


Here's an example of mine that works perfectly, the protocol definition first of all:

@class ExampleClass;

@protocol ExampleProtocol

@required

// Properties
@property (nonatomic, retain) ExampleClass *item;

@end

Below is a working example of a class supporting this protocol:

#import <UIKit/UIKit.h>
#import "Protocols.h"

@class ExampleClass;

@interface MyObject : NSObject <ExampleProtocol> {

    // Property backing store
    ExampleClass        *item;

}


@implementation MyObject

// Synthesize properties
@synthesize item;

@end

all you have to do really is to drop a

@synthesize title;

in your implementation and you should be all set. it works the same way as just putting the property in your class interface.

Edit:

You may want to do this more specifically:

@synthesize title = _title;

This will fall in line with how xcode's automatic synthesis creates properties and ivars if you use auto-synthesis, so that way if your class has properties from a protocol and a class, some of your ivars won't have the different format which could impact readability.


Take a look at my article PROPERTY IN PROTOCOL

Suppose I have MyProtocol that declares a name property, and MyClass that conforms to this protocol

Things worth noted

  1. The identifier property in MyClass declares and generates getter, setter and backing _identifier variable
  2. The name property only declares that MyClass has a getter, setter in the header. It does not generate getter, setter implementation and backing variable.
  3. I can’t redeclare this name property, as it already declared by the protocol. Do this will yell an error

    @interface MyClass () // Class extension
    
    @property (nonatomic, strong) NSString *name;
    
    @end
    

How to use property in protocol

So to use MyClass with that name property, we have to do either

  1. Declare the property again (AppDelegate.h does this way)

    @interface MyClass : NSObject <MyProtocol>
    
    @property (nonatomic, strong) NSString *name;
    
    @property (nonatomic, strong) NSString *identifier;
    
    @end
    
  2. Synthesize ourself

    @implementation MyClass
    
    @synthesize name;
    
    @end