在Objective C的头文件中隐藏实例变量

发布时间:2023-02-17 / 作者:清心寡欲
本文介绍了在Objective C的头文件中隐藏实例变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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;

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

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.

推荐答案

对于 64 位应用程序和 iPhone 应用程序(虽然不在模拟器中),属性合成也能够合成实例变量的存储.

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.

即这行得通:

@interface MyClass : MySuperClass 
{ 
    //nothing here
}

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

@implementation MyClass
@synthesize anObject;
@end

如果你为 32 位 Mac OS X 或 iPhone Simulator 编译,编译器会报错.

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

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



[英文标题]Hide instance variable from header file in Objective C


声明:本媒体部分图片、文章来源于网络,版权归原作者所有,如有侵权,请联系QQ:330946442删除。