开发者

Categories & singelton in cocoa

I have a SingletonClass, which has only one instance. I want to expand it and add to it a category (MoreFeatures).

I created a source SingletonClass+MoreFeatures, which interface/implementation looks like that:

@interface SingletonClass (MoreFeatures)

- (void) feature;

@end

@implementation SingletonClass (MoreFeatures)
- (void) feature
{
}
@end

When I created my SingletonClass i followed the Apple's example of Singleton implementation in cocoa.

This singleton resides in a project B, which is a dependency of a bigger project A. I can call for [[SingletonClass sharedInstance] foo]开发者_Python百科 from a source in project A and it works in case that function foo is located in SingletonClass interface/implementation. When I try to call [[SingletonClass sharedInstance] feature] from a source in project A, I get a crash, which says that there is no method feature for class SingletonClass...

Why it happens?

Thanks a lot,

Nava

EDIT: The crash happens in a runtime. The category file source resides in project B along with a SingletonClass


You need to specify the -all_load and the -ObjC linker flags for your project.

Hope this helps, Moszi


It sounds like your category implementation isn't being compiled when you build project A. This sort of thing is definitely possible, Foundation/AppKit/UIKit make heavy use of categories to split large classes across multiple files.

You don't make it clear how your project B is included in project A. If it's a framework, shared or static library, then make sure your category file is included in the project B "compile sources" build phase. If it's included as source, make sure you compile that file as part of the project A build.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜