objective-c中扩展内的变量

时间:2022-09-07 19:34:14

I want to know how the odometer variable behaves in the following example.I mean since its declared within an extension is it private?

我想知道里程表变量在下面的例子中是如何表现的。我的意思是因为它在扩展中声明它是私有的吗?

 //In Car.m
#import "Car.h"
@interface Car ()
@property (readonly) double odometer;
-(BOOL)engineIsWorking;
@end

2 个解决方案

#1


2  

The variables/methods declared in the extension are only visible in the compilation units that they are imported into or declared within.

扩展中声明的变量/方法仅在导入或声明它们的编译单元中可见。

For example, you could put your extensions in a file called Car_Private.h, then #import "Car_Private.h" in both Car.m (so the @implementation Car automatically synthesizes storage and methods) and Tire.m. By doing so, Tire.m would effectively have access to engineIsWorking and odometer.

例如,您可以将扩展名放在名为Car_Private.h的文件中,然后将#import“Car_Private.h”放在Car.m中(以便@implementation Car自动合成存储和方法)和Tire.m.通过这样做,Tire.m可以有效地访问engineIsWorking和里程表。

That is, extensions are only as "private" as you want them to be. Note that nothing is truly private in Objective-C; it is only private at compilation time.

也就是说,扩展只是像你想要的那样“私有”。请注意,Objective-C中没有任何内容真正是私有的;它只在编译时私有。

#2


1  

Yes. As your are declaring the extension in you .m file, and you never #import this .m file anywhere (you #import the .h), only the code within the .m file has access to this extension.

是。当您在.m文件中声明扩展名时,您永远不会#import这个.m文件(#import .h),只有.m文件中的代码才能访问此扩展名。

As stated in the docs:

如文档中所述:

Class extensions are often used to extend the public interface with additional private methods or properties for use within the implementation of the class itself.

类扩展通常用于使用其他私有方法或属性扩展公共接口,以便在类本身的实现中使用。

#1


2  

The variables/methods declared in the extension are only visible in the compilation units that they are imported into or declared within.

扩展中声明的变量/方法仅在导入或声明它们的编译单元中可见。

For example, you could put your extensions in a file called Car_Private.h, then #import "Car_Private.h" in both Car.m (so the @implementation Car automatically synthesizes storage and methods) and Tire.m. By doing so, Tire.m would effectively have access to engineIsWorking and odometer.

例如,您可以将扩展名放在名为Car_Private.h的文件中,然后将#import“Car_Private.h”放在Car.m中(以便@implementation Car自动合成存储和方法)和Tire.m.通过这样做,Tire.m可以有效地访问engineIsWorking和里程表。

That is, extensions are only as "private" as you want them to be. Note that nothing is truly private in Objective-C; it is only private at compilation time.

也就是说,扩展只是像你想要的那样“私有”。请注意,Objective-C中没有任何内容真正是私有的;它只在编译时私有。

#2


1  

Yes. As your are declaring the extension in you .m file, and you never #import this .m file anywhere (you #import the .h), only the code within the .m file has access to this extension.

是。当您在.m文件中声明扩展名时,您永远不会#import这个.m文件(#import .h),只有.m文件中的代码才能访问此扩展名。

As stated in the docs:

如文档中所述:

Class extensions are often used to extend the public interface with additional private methods or properties for use within the implementation of the class itself.

类扩展通常用于使用其他私有方法或属性扩展公共接口,以便在类本身的实现中使用。