Laur Stefan
Laur Stefan

Reputation: 1589

Meaning of the angle brackets in Objective-C?

Even though the question is quite wide I am actually curious regarding one case I sow recently while using the Realm library. As I previously used the protocols(delegate) on many occasions and also imported classes using <>. And now this is the line the code I don't completely understand or don't understand at all if I am mistaking:

@property (nonatomic, strong) RLMArray <ExerciseLog *><ExerciseLog> * exerciseLogs;

I suppose that the second part of the line <ExerciseLog> * exerciseLogs is used to ensure that exerciseLogs may be an instance of any ExerciseLog that conforms to the ExerciseLog protocol, is my assumption correct?

Or simple said if the user send a different object then the expected one, the app won't crash, and that a default value will be assigned.

And this part I am guessing, the is some sort of safe casting so that the returned object confirms to the ExerciseLog.

Upvotes: 2

Views: 1166

Answers (2)

Sulthan
Sulthan

Reputation: 130200

A combination of Obj-C protocol conformance and generics. RLMArray is declared as

@interface RLMArray < RLMObjectType : RLMObject * >  : NSObject<RLMCollection,NSFastEnumeration> 

it has one generic argument. That's the <ExerciseLog *>.

The second part <ExerciseLog> is conformance to protocol of the given type.

By the way, that protocol is declared using RLM_ARRAY_TYPE macro. The code seems to be a bit complicated but it was probably an older way to enforce element type for arrays (RLMArray<protocolX> is not assignable to RLMArray<protocolY>).

Quoting the documentation:

Unlike an NSArray, RLMArrays hold a single type, specified by the objectClassName property. This is referred to in these docs as the “type” of the array.

When declaring an RLMArray property, the type must be marked as conforming to a protocol by the same name as the objects it should contain (see the RLM_ARRAY_TYPE macro). RLMArray properties can also use Objective-C generics if available. For example:

Upvotes: 3

typedef
typedef

Reputation: 937

The angle brackets in a class interface definition indicates the protocols that your class is conforming to.

A protocol is almost like an interface in Java or C#, with the addition that methods in an Objective-C protocol can be optional.

Additionaly in Objective-C you can declare a variable, argument or instance variable to conform to several protocols as well. Example

NSObject *myVariable; In this case the class must be NSObject or a subclass (only NSProxy and its subclasses would fail), and it must also conform to both NSCoding and UITableViewDelegate protocols.

In Java or C# this would only be possible by actually declaring said class.

Upvotes: 1

Related Questions