可能重复:
objective-c中的受保护方法
声明私有属性的方法很简单.
您声明在.m文件中声明的扩展名中.
假设我想声明受保护的属性并从类和子类访问它.
这是我试过的:
//
// BGGoogleMap+protected.h
//
//
#import "BGGoogleMap.h"
@interface BGGoogleMap ()
@property (strong,nonatomic) NSString * protectedHello;
@end
Run Code Online (Sandbox Code Playgroud)
那是编译.然后我补充说:
#import "BGGoogleMap+protected.h"
@implementation BGGoogleMap ()
-(NSString *) protectedHello
{
return _
}
@end
Run Code Online (Sandbox Code Playgroud)
问题开始了.我似乎无法在原始的.m文件之外实现类扩展.Xcode将要求该括号内的东西.
如果我做
#import "BGGoogleMap+protected.h"
@implementation BGGoogleMap (protected)
-(NSString *) protectedHello
{
return _
}
@end
Run Code Online (Sandbox Code Playgroud)
我无法访问BGGoogleMap + protected.h中声明的_protectedHello的ivar
当然我可以使用常规类别而不是扩展名,但这意味着我不能拥有受保护的属性.
所以我该怎么做?