我一直在尝试在 NSArray 上创建一个类别并查看 NSArray 的接口(interface)我假设添加一个返回 ObjectType 的方法是:
// interface
- (nullable ObjectType)giveMeAnObject;
// implementation
- (nullable ObjectType)giveMeAnObject
{
ObjectType object = nil;
return object;
}
但是这不起作用,我在返回类型中收到错误消息 Expected ')'。
最佳答案
看来您可以在接口(interface)中使用轻量级泛型,但不能在实现中使用。
@interface NSArray<ObjectType> (MyAdditions)
- (nullable ObjectType)giveMeAnObject; // specify return type
@end
@implementation NSArray (MyAdditions)
- (id)giveMeAnObject // use id for the implementation
{
return nil;
}
@end
您可能想要提交 enhancement request如果你也想在 @implementation
block 中看到它们。
关于objective-c - 有没有办法在 NSArray 的类别中使用 ObjectType?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32673975/