如何在Objective-C中获取对象的地址

时间:2022-04-22 16:58:17

I have to call an objective C method from a cpp Function.

我必须从cpp函数调用一个客观的C方法。

I have a class C, whose object address is required in this function. I did come across another link which guided me on how to have a reference to the class C, and use it for invocation from the cpp function.

我有一个C类,在这个函数中需要它的对象地址。我确实遇到了另一个链接,它引导我如何引用C类,并将其用于cpp函数的调用。

In my case, there is one small difference in that the Class C is already instantiated, and I would not want to allocate an object again. So how can I get its object address?

在我的例子中,有一个小的区别在于C类已经被实例化了,我不想再次分配一个对象。那么我怎样才能得到它的对象地址?

The code looks like this:

代码如下所示:

C.h

import Cocoa/Cocoa.h

id refToC

@interface C: NSObject
{

;

somemethod;

;
}

@end

C.m

@implementation C

- (void) somemethod
{
;
;
}

@end

B.mm

import C.h

void func()
{

//I need the address of object here, so as to invoke:
[refToC somemethod];
}

Thanks in Advance

提前致谢

~ps7

3 个解决方案

#1


The id type is already a pointer to an object. Once you have created a valid object, e.g.:

id类型已经是指向对象的指针。一旦你创建了一个有效的对象,例如:

refToC = [[C alloc] init]

#2


The easiest way is to make use of the singleton design pattern. Here's a common way to make use of that pattern in Objective-C:

最简单的方法是使用单例设计模式。以下是在Objective-C中使用该模式的常用方法:

Widget.h

@interface Widget : NSObject {
    // ...
}

// ...

- (void)someMethod;
+ (Widget *)sharedWidget;

@end

Widget.m

@implementation Widget

// ...

+ (Widget *)sharedWidget {
    static Widget *instance;

    @synchronized (self) {
        if (!instance)
           instance = [[Widget alloc] init];
    }

    return instance;
}

@end

CppWrapper.mm

void methodWrapper() {
    [[Widget sharedWidget] someMethod];
}

#3


Thanks a lot for your pointers. I had missed telling that the class C is a controller class. I tried assigning refToC to self in awakeFromNib, and the invocation in func() worked like a charm.

非常感谢你的指点。我错过了告诉C类是控制器类。我尝试在awakeFromNib中为自己分配refToC,而func()中的调用就像一个魅力。

Thanks Matt and John for your pointers.

感谢Matt和John的指示。

~ ps7

#1


The id type is already a pointer to an object. Once you have created a valid object, e.g.:

id类型已经是指向对象的指针。一旦你创建了一个有效的对象,例如:

refToC = [[C alloc] init]

#2


The easiest way is to make use of the singleton design pattern. Here's a common way to make use of that pattern in Objective-C:

最简单的方法是使用单例设计模式。以下是在Objective-C中使用该模式的常用方法:

Widget.h

@interface Widget : NSObject {
    // ...
}

// ...

- (void)someMethod;
+ (Widget *)sharedWidget;

@end

Widget.m

@implementation Widget

// ...

+ (Widget *)sharedWidget {
    static Widget *instance;

    @synchronized (self) {
        if (!instance)
           instance = [[Widget alloc] init];
    }

    return instance;
}

@end

CppWrapper.mm

void methodWrapper() {
    [[Widget sharedWidget] someMethod];
}

#3


Thanks a lot for your pointers. I had missed telling that the class C is a controller class. I tried assigning refToC to self in awakeFromNib, and the invocation in func() worked like a charm.

非常感谢你的指点。我错过了告诉C类是控制器类。我尝试在awakeFromNib中为自己分配refToC,而func()中的调用就像一个魅力。

Thanks Matt and John for your pointers.

感谢Matt和John的指示。

~ ps7