如何在Objective-C中创建全局函数

时间:2021-08-06 05:38:29

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

我正在开发一个iphone应用程序,我需要在我的课程中使用一些通用的功能。

But how can I do this?

但是我怎么做呢?

I just tried to create functions.h likes this

我只是想创建函数。h喜欢这个

#include <Foundation/Foundation.h>

- (void)printTest;

and in the functions.m

而在functions.m

#import "functions.h"

- (void)prinTest {
    NSLog(@"test");
}

but it doesn't work. Says me: "Method definition not in a @implementation context".

但它不工作。我说:“方法定义不在@实现上下文中”。

5 个解决方案

#1


48  

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:

第三个选项(不好,但可能)是为您的方法向NSObject添加一个类别:

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];

#2


9  

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

当你想要一个全局函数时,只需要写一个C函数。objective-C语法只在对象的上下文中使用。

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

Edit:

编辑:

You have to add the declaration in functions.h:

你必须在功能上加上声明。h:

void printTest();

#3


3  

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.

# include函数。在所有需要它的课程中,你都应该准备好,我一直都在做。

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:

你必须使用某种对象,但你可以通过使用一类NSObject来让它“感觉”像一个全局objective-c函数:

  • Create a new file and choose Objective C Category.
  • 创建一个新文件并选择Objective - C类别。
  • Make it a category of NSObject.
  • 使它成为NSObject的一个类别。
  • Use the templates provided to define and implement your methods.
  • 使用提供的模板来定义和实现您的方法。

Now you can use them simply by invoking

现在,您可以通过调用来使用它们

[self myCategoryMethod:optionalParameter];

#4


0  

Try to rename functions.m into functions.c.

尝试重命名功能。m 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中,并将它们作为静态的:+ (void)prinTest来拒绝。然后您可以使用此代码访问它们

[SharedClass printTest];

#5


0  

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(其中包含了函数h),请尝试以下方法:

void printTest ()
{
    /* do your print stuff */
}

#1


48  

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:

第三个选项(不好,但可能)是为您的方法向NSObject添加一个类别:

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];

#2


9  

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

当你想要一个全局函数时,只需要写一个C函数。objective-C语法只在对象的上下文中使用。

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

Edit:

编辑:

You have to add the declaration in functions.h:

你必须在功能上加上声明。h:

void printTest();

#3


3  

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.

# include函数。在所有需要它的课程中,你都应该准备好,我一直都在做。

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:

你必须使用某种对象,但你可以通过使用一类NSObject来让它“感觉”像一个全局objective-c函数:

  • Create a new file and choose Objective C Category.
  • 创建一个新文件并选择Objective - C类别。
  • Make it a category of NSObject.
  • 使它成为NSObject的一个类别。
  • Use the templates provided to define and implement your methods.
  • 使用提供的模板来定义和实现您的方法。

Now you can use them simply by invoking

现在,您可以通过调用来使用它们

[self myCategoryMethod:optionalParameter];

#4


0  

Try to rename functions.m into functions.c.

尝试重命名功能。m 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中,并将它们作为静态的:+ (void)prinTest来拒绝。然后您可以使用此代码访问它们

[SharedClass printTest];

#5


0  

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(其中包含了函数h),请尝试以下方法:

void printTest ()
{
    /* do your print stuff */
}