在Objective C中隐藏头文件中的实例变量 [英] Hide instance variable from header file in Objective C

查看:180
本文介绍了在Objective C中隐藏头文件中的实例变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了用Objective C编写的库(我只有头文件和.a二进制文件)。
在头文件中,它是这样的:

  @interface MyClass:MySuperClass 
{
//这里没有
}

@property(nonatomic,retain)MyObject anObject;
- (void)someMethod;

我该如何达到同样的效果?如果我尝试在接口的{}内声明一个没有相应ivar的属性,编译器会给我一个错误。最终,我想在.a中隐藏我的类的内部结构,并将必要的方法暴露给头文件。如何在.m中声明实例变量?类别不允许我添加伊娃,只是方法。

解决方案

对于64位应用程序和iPhone应用程序(尽管不在模拟器),财产综合也能够综合实例变量的存储。

这样做:

  @interface MyClass:MySuperClass 
{
//这里没有
}

@property(nonatomic,retain)MyObject * anObject;
@end

@implementation MyClass
@synthesize anObject;
@end

如果您编译的是32位Mac OS X或iPhone Simulator,编译器会给出错误。


I came across a library written in Objective C (I only have the header file and the .a binary). In the header file, it is like this:

@interface MyClass : MySuperClass 
{ 
    //nothing here
}

@property (nonatomic, retain) MyObject anObject;
- (void)someMethod;

How can I achieve the same thing? If I try to declare a property without its corresponding ivar inside the interface's {}, the compiler will give me an error. Ultimately, I want to hide the internal structure of my class inside the .a, and just expose the necessary methods to the header file. How do I declare instance variables inside the .m? Categories don't allow me to add ivar, just methods.

解决方案

For 64 bit applications and iPhone applications (though not in the simulator), property synthesis is also capable of synthesizing the storage for an instance variable.

I.e. this works:

@interface MyClass : MySuperClass 
{ 
    //nothing here
}

@property (nonatomic, retain) MyObject *anObject;
@end

@implementation MyClass
@synthesize anObject;
@end

If you compile for 32 bit Mac OS X or the iPhone Simulator, the compiler will give an error.

这篇关于在Objective C中隐藏头文件中的实例变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆