Reputation: 11785
Please refer to the following code:
Filename: myclass.h
@interface myclass:NSObject
...
@end
@interface NSObject(CategoryName)
....
@end
I do not understand how can we declare 2 @interface directives in the same .h file? and like in the implementation file we can only implement one of the above interfaces.eg.
Filename: myclass.m
@implementation myclass
...
@end
in the above code, i cannot write @implementation Categoryname as the name of the .h file is myclass. How do i implement the methods described in the category.
Also can my main interface extend one class and the category that i am defining in the same .h file extend another class?
How can i declare one class into another class (nested class) in objective c?
thank you in advance.
UPDATE
After reading the responses, i have 2 more questions
Upvotes: 0
Views: 103
Reputation: 150755
Add it as another declaration in the same file, like this
@implementation NSObject (categoryname)
...
@end
It's all described in the Categories and extensions section of the Developer docs.
Since you are extending a different class in the same interface - you need to have a separate declaration in the implementation file.
Edited to answer the additions:
NSObject+CategoryName.h
and NSObject+CategoryName.m
. Once you have declared and defined a category on a class it is available to all objects of that class. So since you are adding an extension to NSObject, those methods are available to all classes that inherit from NSObject (which is practically all classes) whether or not you import the header file. But, you get a compiler warning if you don't import the header, even though the Objective-C runtime will still allow those methods to be called.Upvotes: 4