Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to use ObjectType in a category on NSArray?

I've been trying to create a category on NSArray and looking at the interface of NSArray I assume that to add a method that returns an ObjectType, is:

// interface
- (nullable ObjectType)giveMeAnObject;

// implementation
- (nullable ObjectType)giveMeAnObject
{
    ObjectType object = nil;
    return object;
}

However that doesn't work and I get the error message Expected ')' in the return type.

like image 481
Patrick Avatar asked Sep 19 '15 22:09

Patrick


1 Answers

It seems you can use lightweight generics in interfaces, but not implementations.

@interface NSArray<ObjectType> (MyAdditions)

- (nullable ObjectType)giveMeAnObject; // specify return type

@end


@implementation NSArray (MyAdditions)

- (id)giveMeAnObject // use id for the implementation
{
    return nil;
}

@end

You might want to file an enhancement request if you'd like to see them in @implementation blocks too.

like image 182
jtbandes Avatar answered Nov 02 '22 20:11

jtbandes