如何在自己的文件中定义 Objective-C 中的 c 结构并将该结构作为参数传递 [英] How to define a c struct in Objective-C in its own file and pass the struct as an argument

查看:37
本文介绍了如何在自己的文件中定义 Objective-C 中的 c 结构并将该结构作为参数传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将这个问题分为两个问题,因为我认为没有另一个问题我无法提出一个问题.

I'm asking this as two questions because I don't think I can ask one without the other.

在我的应用程序中,我有很多东西以ND"为前缀,表示 n 维,并且在我的 NDVector 类中有一个看起来像这样的方法:

In my application, I have a lot of things that are prefixed with "ND" meaning n-dimensional, and I have a method that looks like this in my NDVector class:

angleAtIntersectionWith: (NDVector *) vec 
    inPlaneFirstAxis: (NSUInteger) a1 secondAxis: (NSUInteger) a2

如果你想知道,这个方法应该在一个平面上找到两个向量之间的角度,这意味着只有它们的两个分量,它们被 a1 和 a2 索引.所以,例如.vec1 angleAtIntersectionWith: vec2 inPlanFirstAxis: 0 secondAxis: 2 将在 vec1vec2 之间的 0 索引元素和 2 索引元素(X和 Z 分量).

If you're wondering, this method is supposed to find the angle between two vectors in just one plane, meaning by just two of their components, which are indexed by a1 and a2. So, for example. vec1 angleAtIntersectionWith: vec2 inPlanFirstAxis: 0 secondAxis: 2 will find the angle between vec1 and vec2 at their 0 index element and 2 index element (X and Z components).

我想简化这个方法,使它看起来像这样:

I wanted to simplify this method to make it look like this:

angleAtIntersectionWith: (NDVector *) vec inPlane: (struct NDPlane *) plane

NDPlane 应该是这样的结构:

Where NDPlane should be a structure like this:

struct NDPlane{
    NSUInteger a1;    
    NSUInteger a2;
};

就是这样,这就是它所需要的.没有方法什么的.

And that's it, that's all it should need. No methods or anything.

我想将 NDPlane 保存在自己的文件中,那么我该怎么做呢?我做一个简单的c头文件吗?如果是这样,我是否正确地将其作为结构传递?

I want to keep NDPlane in its own file, so how do I do this? Do I make a simple c header file? If so, am I passing this as struct correctly?

推荐答案

是的,只需创建一个简单的 .h 文件来声明结构.然后在任何需要的地方导入 .h.

Yes, just create a simple .h file that declares the struct. Then import the .h where ever you need it.

但是,您不需要参数中的指针.C 结构体是按值传递的,而不是引用.就像原始类型一样.

However, you don't want the pointer in the argument. C structs are passed by value, not reference. Just like primitive types.

我还建议您对结构进行 typedef,这样您就不需要在使用 NDPlane 的任何地方都使用 struct.

I would also suggest you typedef the struct so you don't need to use struct everywhere you use NDPlane.

这篇关于如何在自己的文件中定义 Objective-C 中的 c 结构并将该结构作为参数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆