有没有一种方法可以在NSArray的类别中使用ObjectType?

24

我一直在尝试在NSArray上创建一个分类,并查看了NSArray的接口,我假设要添加一个返回ObjectType的方法,应该是:


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;
}

然而,这并不起作用,我收到了错误信息“预计‘)’”在返回类型中。

1个回答

33

看起来你可以在接口中使用轻量级泛型,但不能在实现中使用。

@interface NSArray<ObjectType> (MyAdditions)

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

@end


@implementation NSArray (MyAdditions)

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

@end

如果您希望在@implementation块中也看到它们,那么您可能希望提交一个改进请求


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接