是否可以在类外声明Objective-C方法?

时间:2022-01-24 17:58:05

I know that you can declare a C function outside of a class, but is it possible to declare a Objective-C method outside of a class?

我知道你可以在类之外声明一个C函数,但是可以在类之外声明一个Objective-C方法吗?

Example:

// Works
void printHelloC()
{
    NSLog(@"Hello.");
}

// Error
-(void) printHelloOC
{
    NSLog(@"Hello.");
}

int main (int argc, const char * argv[])
{
    @autoreleasepool {
        printHelloC();
        [self printHelloOC];// 'self' obviously would not work but you get the idea
    }
    return 0;
}

5 个解决方案

#1


4  

It depends. You can do something similar with method adding at runtime:

这取决于。你可以在运行时使用方法添加类似的东西:

#import <objc/runtime.h>

void myCustomMethod(id self, SEL _cmd, id arg1, id arg2)
{
    NSLog(@"This is a test, arg1: %@, arg2: %@", arg1, arg2);
}

int main(int argc, char *argv[])
{
    Class NSObjClass = [NSObject class];

    class_addMethod(NSObjClass, @selector(myNewMethod::), (IMP) myCustomMethod, "v@:@@");

    NSObject myObject = [NSObject new];

    [myObject myNewMethod:@"Hi" :@"There"];

    [myObject release];

    return 0;
}

But that is about it outside of a @class construct, and it really just covers up what happens with a category.

但这是关于@class构造之外的事情,它实际上只是掩盖了类别所发生的事情。

#2


4  

You can use a category for this.

您可以使用此类别。

As an instance method:

作为实例方法:

@interface NSObject (MONStuff)
- (void)printHelloOC;
@end

@implementation NSObject (MONStuff)
- (void)printHelloOC
{
  NSLog(@"Hello.");
}
@end

// in use:

NSObject * obj = ...;
[obj printHelloOC];

As a Class method:

作为Class方法:

@interface NSObject (MONStuff)
+ (void)printHelloOC;
@end

@implementation NSObject (MONStuff)
+ (void)printHelloOC
{
  NSLog(@"Hello.");
}
@end

// in use:

[NSObject printHelloOC];

Of course, you must associate that with a class - so it's not exactly the same as you posted, but it's a close definition + declaration separate from the formal class declaration.

当然,您必须将它与一个类相关联 - 因此它与您发布的不完全相同,但它是一个与正式类声明分开的紧密定义+声明。

#3


0  

A method without an associated class is a meaningless concept. Functions, as you've noted, are just fine.

没有相关类的方法是无意义的概念。正如你所指出的那样,功能就好了。

#4


0  

Objective c functions are always associated with a class. If you mean you want to use an objective-c function without instantiating a class, you can of course write a class method (notice the plus sign instead of the usual hyphen)

Objective c函数始终与类相关联。如果你的意思是你想在没有实例化类的情况下使用objective-c函数,你当然可以编写一个类方法(注意加号而不是通常的连字符)

@interface Test

+ (void)aClassMethod;

@end

then you can call it by calling

然后你可以打电话给它

[Test aClassMethod];

#5


0  

No, it is not possible - you will need to either use global C functions or class (+) methods.

不,这是不可能的 - 您将需要使用全局C函数或类(+)方法。

#1


4  

It depends. You can do something similar with method adding at runtime:

这取决于。你可以在运行时使用方法添加类似的东西:

#import <objc/runtime.h>

void myCustomMethod(id self, SEL _cmd, id arg1, id arg2)
{
    NSLog(@"This is a test, arg1: %@, arg2: %@", arg1, arg2);
}

int main(int argc, char *argv[])
{
    Class NSObjClass = [NSObject class];

    class_addMethod(NSObjClass, @selector(myNewMethod::), (IMP) myCustomMethod, "v@:@@");

    NSObject myObject = [NSObject new];

    [myObject myNewMethod:@"Hi" :@"There"];

    [myObject release];

    return 0;
}

But that is about it outside of a @class construct, and it really just covers up what happens with a category.

但这是关于@class构造之外的事情,它实际上只是掩盖了类别所发生的事情。

#2


4  

You can use a category for this.

您可以使用此类别。

As an instance method:

作为实例方法:

@interface NSObject (MONStuff)
- (void)printHelloOC;
@end

@implementation NSObject (MONStuff)
- (void)printHelloOC
{
  NSLog(@"Hello.");
}
@end

// in use:

NSObject * obj = ...;
[obj printHelloOC];

As a Class method:

作为Class方法:

@interface NSObject (MONStuff)
+ (void)printHelloOC;
@end

@implementation NSObject (MONStuff)
+ (void)printHelloOC
{
  NSLog(@"Hello.");
}
@end

// in use:

[NSObject printHelloOC];

Of course, you must associate that with a class - so it's not exactly the same as you posted, but it's a close definition + declaration separate from the formal class declaration.

当然,您必须将它与一个类相关联 - 因此它与您发布的不完全相同,但它是一个与正式类声明分开的紧密定义+声明。

#3


0  

A method without an associated class is a meaningless concept. Functions, as you've noted, are just fine.

没有相关类的方法是无意义的概念。正如你所指出的那样,功能就好了。

#4


0  

Objective c functions are always associated with a class. If you mean you want to use an objective-c function without instantiating a class, you can of course write a class method (notice the plus sign instead of the usual hyphen)

Objective c函数始终与类相关联。如果你的意思是你想在没有实例化类的情况下使用objective-c函数,你当然可以编写一个类方法(注意加号而不是通常的连字符)

@interface Test

+ (void)aClassMethod;

@end

then you can call it by calling

然后你可以打电话给它

[Test aClassMethod];

#5


0  

No, it is not possible - you will need to either use global C functions or class (+) methods.

不,这是不可能的 - 您将需要使用全局C函数或类(+)方法。