iphone - Objective-C Protocol Madness——如何根据协议(protocol)返回对象?

标签 iphone objective-c protocols

@protocol Eating
@end

@interface Eat : NSObject<Eating>
{
}
- (id<Eating> *)me;
@end

@implementation Eat
- (id<Eating> *)me { return self; }
@end

在上面的一段 Objective-C 代码中,为什么“return self”会导致“Return from incompatible pointer type”警告?什么是不兼容的指针类型以及如何修复它?

最佳答案

因为 id 本身就是一个指针,所以不需要星号。

@interface Eat : NSObject<Eating> {
}
- (id<Eating>)me;
@end

关于iphone - Objective-C Protocol Madness——如何根据协议(protocol)返回对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1386791/

相关文章:

objective-c - 创建一个 Objective-C API

ios - Swift:检查有一个协议(protocol)并像在 Java 中一样使用 getter

iphone - iOS 扫描广播 channel

iphone - 如何从 NSMutableArray 访问 NSObject 类变量进行排序

iphone - 使用 Curl 验证 App Store 收据时出现 IllegalStateException?

php - EasyAPNS 在生产中获取排队消息时 - 空 clientid 列

iphone - 用于在 iPhone 中调用电话的 Skype URL 处理程序是什么?

iphone - NSMutableArray - 删除重复项

ios - 如何使用协议(protocol)/委托(delegate)在 UIViewController 之间传递数据

python - 在 Python 中循环遍历 Protocol Buffers 属性