How to create global functions in Objective-C

前端 未结 5 1639
攒了一身酷
攒了一身酷 2020-11-30 02:03

I\'m developing an iphone app and I need to have some functions to use globally in my classes.

But how can I do this?

I just tried to create functions.

相关标签:
5条回答
  • 2020-11-30 02:25

    When you want a global function, just write a regular C function. The Objective-C syntax is meant to be used solely in the context of object methods.

    void printTest() {
        NSLog(@"This is a test");
    }
    

    You also have to add the declaration in the functions.h header:

    void printTest();
    
    0 讨论(0)
  • Simple. Your setup is almost perfect.

    Just #include your Functions.h in all classes that need it, and you should be all set. I do it all the time.

    You will have to use some kind of object, but you can make it "feel" just like a global objective-c function by using a category of NSObject:

    • Create a new file and choose Objective C Category.
    • Make it a category of NSObject.
    • Use the templates provided to define and implement your methods.

    Now you can use them simply by invoking

    [self myCategoryMethod:optionalParameter];
    
    0 讨论(0)
  • 2020-11-30 02:31

    Try to rename functions.m into functions.c.

    Or add this methods into some class SharedClass and decline them as static : + (void)prinTest. Then you can access them using this code

    [SharedClass printTest];
    
    0 讨论(0)
  • 2020-11-30 02:34

    What your error is referring to is you need to have that method in an interface.

    @interface SomeClass
    - (void)printTest;
    @end
    

    To use a static void throughout your app (where you have included your Functions.h), try the following:

    void printTest ()
    {
        /* do your print stuff */
    }
    
    0 讨论(0)
  • 2020-11-30 02:37

    Two options here. First is create a class method in a static class:

    Header:

    #import <Foundation/Foundation.h>
    
    @interface GlobalStuff : NSObject {}
    
    + (void)printTest;
    
    @end
    

    Implementation:

    #import "functions.h"
    
    @implementation GlobalStuff
    
    + (void) printTest {
      NSLog(@"test");
    }
    

    Call using:

    #import "functions.h"
    
    ...
    [GlobalStuff printTest];
    

    The other option is to declare a global function instead of class:

    Header:

    void GSPrintTest();
    

    Implementation:

    #import <Foundation/Foundation.h>
    #import "functions.h"
    void GSPrintTest() {
      NSLog(@"test");
    }
    

    Call using:

    #import "functions.h"
    ...
    GSPrintTest();
    

    A third (bad, but possible) option would be adding a category to NSObject for your methods:

    Header:

    #import <Foundation/Foundation.h>
    
    @interface NSObject(GlobalStuff)
    - (void) printTest;
    @end
    

    Implementation:

    #import "functions.h"
    
    @implementation NSObject(GlobalStuff)
    - (void) printTest {
      NSLog(@"test");
    }
    @end
    

    Call using:

    #import "functions.h"
    ...
    [self printTest];
    
    0 讨论(0)
提交回复
热议问题