获取 Objective-c 属性的地址(这是一个 C 结构体)

发布于 2024-07-26 17:28:35 字数 562 浏览 15 评论 0原文

我有一个 Objective-C 类,其中包含一个 C 风格的结构。 我需要调用一个 C 函数,传递一个指向该对象成员(也称为属性)的指针。 对于我的一生,我无法弄清楚如何获取这个 C 结构的地址。 使用传统的 & 运算符来获取地址,我收到 LValue 编译器错误。

typedef struct _myStruct
{
   int aNumber;
}MyStruct, *pMyStruct;

@interface MyClass : NSObject {
    MyStruct mystruct;
}
@property (readwrite) MyStruct myStruct;
@end

以下代码会导致编译器错误:

MyClass* myClass = [[MyClass alloc] init];

MyStruct* p = &(myClass.myStruct);

如何获取指向 myClass 对象的 myStruct 成员的指针?

I have an Objective-C class which contains a C-style struct. I need to call a C function passing a pointer to this object member (a.k.a. property). For the life of me, I can't figure out how to get the address of this C struct. Using the traditional & operator to get the address, I'm getting an LValue compiler error.

typedef struct _myStruct
{
   int aNumber;
}MyStruct, *pMyStruct;

@interface MyClass : NSObject {
    MyStruct mystruct;
}
@property (readwrite) MyStruct myStruct;
@end

The following code results in a compiler error:

MyClass* myClass = [[MyClass alloc] init];

MyStruct* p = &(myClass.myStruct);

How do I get a pointer to the myStruct member of the myClass object?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

记忆之渊 2024-08-02 17:28:35

考虑到 Objective-C 应用程序通常必须与采用指向结构和类似类型的指针的 C/C++ API 一起工作,通常有充分的理由按照原始帖子的要求进行操作,但在 Cocoa 应用程序中,您通常会想要将此类数据存储在 Objective-C 类中以进行数据管理、数组和字典中的集合等。

虽然这个问题已经提出了一段时间,但我没有看到明确的答案,即:你可以有一个方法来返回支持您的财产的数据,但在该方法中不要使用“self”,否则它将通过访问器但仍然无法工作。

- (const MyStruct*) getMyStructPtr
{
    return &mystruct;
}

请注意,我使用的是 OP 中声明的属性,但没有将其引用为 self.mystruct,这会生成编译器错误(因为这会调用合成的 getter 方法)。

There are often pretty good reasons to do what the original post is asking, given that Objective-C apps often have to work with C/C++ API's that take pointers to structs and similar types, but in a Cocoa app you'll often want to store such data in Objective-C classes for data management, collection in arrays and dictionaries, etc.

Though this question has been up for awhile I don't see the clear answer, which is: you can have a method that returns the address of the data that's backing your property, but in that method don't use "self" or it will go through the accessor and still not work.

- (const MyStruct*) getMyStructPtr
{
    return &mystruct;
}

Note that I'm using the declared property from the OP, but not referencing it as self.mystruct, which would generate a compiler error (because that invokes the synthesized getter method).

極樂鬼 2024-08-02 17:28:35

MyStruct mystructMyClass 中是私有的,我假设当您执行 myClass.myStruct 时,您仅引用生成的访问器方法而不是实际的结构。

我认为您无法从外部访问实例变量(在本例中为结构),因为它是私有的。

MyStruct mystruct is private in MyClass, I assume when you do myClass.myStruct you are only refering to generated accessor method not the actual structure.

I don't think you can access the instance variable (structure in this case) from outside because it is private.

诗笺 2024-08-02 17:28:35

要获取指向 myStruct 实例变量的指针,您需要编写一个返回指向该实例变量的指针的方法。

- (void)getMyStructPointer:(MyStruct **)outStruct {
    *outstruct = &myStruct;
}

不过,我真的不认为这是一个好主意。 其他对象不应该从其下面改变该对象的 ivar,这是您可以使用指向结构的指针执行的唯一操作,而不能使用按值返回的结构的副本执行此操作。

To get a pointer to the myStruct instance variable, you need to write a method that returns a pointer to that instance variable.

- (void)getMyStructPointer:(MyStruct **)outStruct {
    *outstruct = &myStruct;
}

I don't really think this is a good idea, though. Other objects should not be mutating that object's ivar out from under it, and that's the only thing you can do with a pointer to the struct that you can't do with a copy of the struct returned by value.

孤檠 2024-08-02 17:28:35

这个问题本身表明至少对术语缺乏理解。

属性是一个接口,由对象公开的两个(或一个只读)方法组成,即 getter 和 setter 方法,在这种情况下:

- (MyStruct) myStruct;
- (void) setMyStruct: (MyStruct) newMyStruct;

谈论“获取属性的地址”是没有意义的。

您可以获取实例变量 (ivar) 的地址。 在本例中,您有一个名为 mystruct 的 ivar,您可以在 MyClass 的方法中使用 &mystruct 获取它的地址。 由于它被标记为@protected(默认情况下),因此您可以使用&self->mystruct在子类中获取它的地址。 如果您将其标记为@public,那么您可以使用&myobj->mystruct获取它的地址。 这是一个糟糕的主意,你真的应该重新考虑一下,但你可以做到。

您可以这样做,但这会很不寻常,并且您最好编写一个显式命名的方法,例如:

- (MyStruct*) getAddressForSettingMyStruct;

如果您只是想要 ivar 的地址用于某些短暂的目的(例如,如果 MyStruct 很大) , 如果它只是只读,则更好的是使用 const MyStruct*。

The question itself demostrates a lack of understanding of at least the terminology.

A property is an interface consisting of two (or one for readonly) methods made public by the object, namely the getter and setter methods, in this case:

- (MyStruct) myStruct;
- (void) setMyStruct: (MyStruct) newMyStruct;

It makes no sense to talk about "taking the address of a property".

You can take the address of an instance variable (ivar). In this case you have an ivar named mystruct, and you can take the address of it with &mystruct in a method of MyClass. Since it is marked @protected (by default), you can take the address of it in a subclass using &self->mystruct. If you mark it @public, then you could take the address of it using &myobj->mystruct. This is a terrible idea, and you should really really rethink this, but you could do it.

If you just want the address of the ivar for some short lived purpose (for example, if MyStruct was large) you could do this, but it would be very unusual, and you'd be better off writing an explicitly named method like:

- (MyStruct*) getAddressForSettingMyStruct;

and if it is just read only, even better would be to use const MyStruct*.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文