如何在 Objective-C 中(按值)传递结构体?
这件事快把我逼疯了!我有一个结构:
typedef struct{
int a;
}myStruct;
然后我有:
myStruct tempStruct;
我试图将该结构传递给一个类方法,其实现是:
- (void) myFunc:(struct myStruct)oneOfMyStructs{};
我像这样调用该方法:
[myClass myFunc:(struct myStruct)tempStruct];
编译器抱怨“请求转换为非标量类型”。我想做的就是将一个结构传递给一个类方法,但语法让我有点困惑。我是 Objective-C 的新手。如果您能看到我哪里出错了,请告诉我。我也无法通过参考,所以如果你能帮助我解决这个问题,那就太好了!
谢谢!
This one has been driving me mad! I have a struct:
typedef struct{
int a;
}myStruct;
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您已将其
typedef
-ed 为myStruct
,因此您不需要(确实不能)在函数中包含struct
关键字签名或电话:应该足够了。 (比较所有那些采用 NSRect 之类的 Cocoa 函数。)
Since you have
typedef
-ed it tomyStruct
, you don't need to (indeed mustn't) include thestruct
keyword in the function signature or the call:should be sufficient. (Compare all those Cocoa functions that take things like
NSRect
s.)