开发者

iOS Static Library And Method Hiding

I'm working on a static library for iOS and want to implement some methods that are public for use by the other classes in the library but not outwardly visible. Basically, I w开发者_Python百科ant to create something similar to Apple's "private" API calls.

I'm ok with someone being able to call these methods given that they discover their name, but I just want to mask them from the header file that's packaged with the library so that XCode doesn't give them the option to call those methods by default, possibly throwing a warning.

Any idea how this can be accomplished?


One idea is the following - You can define the "private" methods of your class in a separate category in a separate .h file. For example, you could have class A looking like the following:

A.h

@interface A : NSObject {
}
- (void) foo; //public method
@end

Now the private methods of A can be declared in a different file:

A+Internal.h

@interface A (Internal) 

- (void) bar; //private method

@end

The implementation file of A (A.m) can have implementations for both sets of methods.

In your static library, you do not publish the header files with "+Internal" in the name. Users of your library will only see methods in A.h, but classes inside your library can use both.


You can declare your "private" methods in a category @interface MyClass(PrivateMethods)or a class extension @interface MyClass() at the top of the .m file or in its own private header file.

From Extensions:

Class extensions are like anonymous categories, except that the methods they declare must be implemented in the main @implementation block for the corresponding class.

It is common for a class to have a publicly declared API and to then have additional methods declared privately for use solely by the class or the framework within which the class resides. You can declare such methods in a category (or in more than one category) in a private header file or implementation file as mentioned above. This works, but the compiler cannot verify that all declared methods are implemented.

Class extensions allow you to declare additional required methods for a class in locations other than within the primary class @interface block.

See also: Best way to define private methods for a class in Objective-C

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜