开发者

How do I pass (by value) a struct in Objective-C?

开发者 https://www.devze.com 2022-12-29 08:54 出处:网络
This one has been driving me mad!I have a struct: typedef struct{ int a; }myStru开发者_如何学JAVAct;

This one has been driving me mad! I have a struct:

typedef struct{
  int a;
}myStru开发者_如何学JAVAct;

Then I have:

myStruct tempStruct;

I am trying to pass the struct to a class method whose implementation is:

- (void) myFunc:(struct myStruct)oneOfMyStructs{};

I call the method like so:

[myClass myFunc:(struct myStruct)tempStruct];

The compiler complains about "Conversion to non-scalar type requested." All I want to do is pass a struct into a class method, but the syntax has me a bit confused. I'm new to Objective-C. Please let me know if you can see where I'm going wrong. I've not been able to pass by reference either, so if you could help me out with that, that would be great!

Thanks!


Since you have typedef-ed it to myStruct, you don't need to (indeed mustn't) include the struct keyword in the function signature or the call:

typedef struct tag_myStruct
{
    int a;
}
myStruct;

- (void) myFunc:(myStruct) x {}

[myClass myFunc:tmpStruct];

should be sufficient. (Compare all those Cocoa functions that take things like NSRects.)

0

精彩评论

暂无评论...
验证码 换一张
取 消