在Objective-C中使用类扩展来选择性可见性

时间:2022-09-07 07:38:37

Would it make any sense to put class extensions in their own .h files and #import them selectively to get various levels of visibility for a class' methods and properties? If this is a bad idea (or would not work), why?

将类扩展放在他们自己的.h文件中并且#import选择性地为类的方法和属性获得不同级别的可见性是否有意义?如果这是一个坏主意(或不起作用),为什么?

2 个解决方案

#1


15  

It is a great idea and exactly why Class Extensions were designed (and why they are different than categories).

这是一个很好的主意,也正是为什么要设计Class Extensions(以及为什么它们与类别不同)。

Namely, you can:

即,你可以:

Foo.h

@interface Foo:NSObject
...public API here...
@property(readonly, copy) NSString *name;
@end

Foo_FrameworkOnly.h

@interface Foo()
@property(readwrite, copy) NSString *name;
@end

Foo.m

#import "Foo.h"
#import "Foo_FrameworkOnly.h"

@interface Foo()
... truly implementation private gunk, including properties go here ...
@end

@implementation Foo
@synthesize name = name_;
@end

And effectively have a property that is publicly readonly and privately read write for only the implementation files that import Foo_FrameworkOnly.h.

并且有效地具有公开只读和私有读取的属性,仅用于导入Foo_FrameworkOnly.h的实现文件。

#2


-2  

Class extension (as opposed to subclassing) in Objective-C is accomplished with Categories. In Xcode, go to File > New > File and select Objective-C Category. It will ask you what to call the category and what class it should extend. You'll get a .h/.m pair in which to put your interface and implementation, respectively. If you want access to the features provided in your extension, just import its .h file.

Objective-C中的类扩展(与子类相反)是使用Categories完成的。在Xcode中,转到File> New> File并选择Objective-C Category。它将询问您将该类别称为什么以及它应该扩展到哪个类。您将获得一个.h / .m对,分别用于放置您的接口和实现。如果要访问扩展中提供的功能,只需导入其.h文件即可。

#1


15  

It is a great idea and exactly why Class Extensions were designed (and why they are different than categories).

这是一个很好的主意,也正是为什么要设计Class Extensions(以及为什么它们与类别不同)。

Namely, you can:

即,你可以:

Foo.h

@interface Foo:NSObject
...public API here...
@property(readonly, copy) NSString *name;
@end

Foo_FrameworkOnly.h

@interface Foo()
@property(readwrite, copy) NSString *name;
@end

Foo.m

#import "Foo.h"
#import "Foo_FrameworkOnly.h"

@interface Foo()
... truly implementation private gunk, including properties go here ...
@end

@implementation Foo
@synthesize name = name_;
@end

And effectively have a property that is publicly readonly and privately read write for only the implementation files that import Foo_FrameworkOnly.h.

并且有效地具有公开只读和私有读取的属性,仅用于导入Foo_FrameworkOnly.h的实现文件。

#2


-2  

Class extension (as opposed to subclassing) in Objective-C is accomplished with Categories. In Xcode, go to File > New > File and select Objective-C Category. It will ask you what to call the category and what class it should extend. You'll get a .h/.m pair in which to put your interface and implementation, respectively. If you want access to the features provided in your extension, just import its .h file.

Objective-C中的类扩展(与子类相反)是使用Categories完成的。在Xcode中,转到File> New> File并选择Objective-C Category。它将询问您将该类别称为什么以及它应该扩展到哪个类。您将获得一个.h / .m对,分别用于放置您的接口和实现。如果要访问扩展中提供的功能,只需导入其.h文件即可。