gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 03:23:47 27 4
gpt4 key购买 nike

@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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com