这是我的问题。
假设我有一个名为WebServiceBase.h的类。我需要在该类中添加一个名为NSString *requestData的iVar。但我不需要将该iVar添加到头文件中并使其对外部人员可见。(如果我将其作为类库分发)
此外,我还需要能够在从WebServiceBase.h扩展的其他类中访问此requestData iVar。(这些扩展类是我写的。不是来自外部的人)
我尝试在类扩展中声明requestData iVar。但是它对扩展类是不可见的。
有什么解决方案吗?我需要保护我的数据,使其对外部世界隐藏起来。
发布于 2012-07-29 15:52:01
您可以通过@protected关键字将ivars定义为protected,这意味着您的类和所有子类都可以毫无问题地访问它,但编译器不会允许其他不是从基类继承的类这样做:
@interface Foo : NSObject
{
@protected
NSObject *a;
}它就这么简单,并且已经为您提供了Objective-C所能提供的所有安全性。
发布于 2012-07-29 15:31:42
您可以在@implementation块中使用ivar定义块。
发布于 2012-07-29 15:39:45
有两种方式,你可以选择你喜欢的一种。
1).h file
@interface YourClass
{
}
.m file
@interface YourClass ()
@property (nonatomic, retain) NSString *title;
@end
@implementation YourClass
@synthesize title;
//your method
2) .h flie
@interface YourClass
{
}
.m file
@implementation YourClass
{
NSString *title;
}
//your methodhttps://stackoverflow.com/questions/11707495
复制相似问题