Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
128 views
in Technique[技术] by (71.8m points)

ios - Is it possible to use extension methods which are added at implementation file rather than interface file in objective c?

This is header file/Interface file(className.h). Here printSomething method is declared as an extension. And I'll call it later in main.m

extension.h

#import <Foundation/Foundation.h>

@interface extension_class : NSObject

@end


@interface extension_class ()       // This is the external method which is added using extension
- (void) printSomething;
@end

This is the Implementation file(className.m). Here printSomething method is defined.

extension.m

        #import "extensions.h"
        
        @implementation extension_class
        
        - (void) printSomething
        {
            NSLog(@"I'm the method defined inside extension class but declared by using extensions");
        }
        @end
    

So far everything works fine. Now My question is simple why can't I access that printSomething method if I declare(until now declaration and definition was not done in same file) it in implementation file. As in the below code snippet? (Please compare and observe the changes among above two .h and .m files with below ones to get my point)

extension.h

#import <Foundation/Foundation.h>
        
        @interface extension_class : NSObject
        
        @end
    
    

extension.m

    #import "extensions.h"
    
    @interface extension_class ()       // This is the external method which is added using extension
    - (void) printSomething;
    @end

    @implementation extension_class
    - (void) printSomething
    {
        NSLog(@"I'm the method defined inside extension class but declared by using extensions");
    }
    @end

This is main method which is common in both cases.

main.m

#import "extensions.h"
int main()
{
    @autoreleasepool
    {
        extension_class *object = [[extension_class alloc]init];
        [object printSomething];
    }
    return 'O';
}

So what is point in having extensions in objective C if it doesn't allow us to add methods anywhere we like? or Is there any other method to achieve what I said above?

question from:https://stackoverflow.com/questions/66046674/is-it-possible-to-use-extension-methods-which-are-added-at-implementation-file-r

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You're free to define extensions in the .m file. This is incredibly common. Those extensions won't generally be known to importers of the .h file, so they won't easily be callable from other files. That's a good thing. It lets us make "private" extensions, which is very useful and common.

They're not really private. Anything can call anything in ObjC. Outside callers just won't know about the method. But they can declare the method themselves as a category (note the text inside the parentheses) and call it:

OtherClass.m

#import "ExtensionClass.h"

@interface ExtensionClass (ThingsIKnowYouHave)
- (void) printSomething;
@end

...

[extensionClass printSomething];

Or they could of course just call it directly without declaring it (though this can cause ARC problems, so avoid this in modern ObjC):

[(id)extensionClass printSomething];

Or they could call it as a selector (again, this can cause ARC problems; so using the category is best):

[extensionClass performSelector: NSSelectorFromString(@"printSomething")];

There's really not much point to creating extensions in the header file (i.e. "public" extensions). If it's in the header file, you might as well just put it in the interface. The most common use of extensions (basically why they were invented), is so you can define methods inside the implementation file.

Extensions shouldn't be confused with categories, where there is text inside the parentheses. These were created to help organize large classes, and later were used for "informal protocols" before @optional was added. Extensions can add methods directly to the base class. Category interfaces just say "this method might exist." Extension interfaces are formal continuations of the primary interface (the compiler requires that they be implemented).

For more on categories and extensions, see Customizing Existing Classes in the Programming with Objective-C guide. See also Defining Classes in the same guide, which may clear up some confusion I believe you have about header files and interfaces.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...