gpt4 book ai didi

objective-c - 如何在 objective-c 中模拟 protected 属性和方法

转载 作者:太空狗 更新时间:2023-10-30 03:36:52 24 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
Protected methods in objective-c

声明私有(private)属性的方式很简单。

您在 .m 文件中声明的扩展名中声明。

假设我想声明 protected 属性并从类和子类访问它。

这是我尝试过的:

//
// BGGoogleMap+protected.h
//
//

#import "BGGoogleMap.h"

@interface BGGoogleMap ()
@property (strong,nonatomic) NSString * protectedHello;
@end

那个是编译。然后我补充说:

#import "BGGoogleMap+protected.h"

@implementation BGGoogleMap ()

-(NSString *) protectedHello
{
return _
}

@end

问题开始了。我似乎无法在原始 .m 文件之外实现类扩展。 Xcode 将要求该括号内的内容。

如果我这样做

#import "BGGoogleMap+protected.h"

@implementation BGGoogleMap (protected)

-(NSString *) protectedHello
{
return _
}

@end

我无法访问 BGGoogleMap+protected.h 中声明的 _protectedHello 的 ivar

当然我可以使用常规类别而不是扩展名,但这意味着我不能拥有 protected 属性。

那我该怎么办呢?

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