使用 C 数组作为属性

发布于 2024-09-29 02:04:33 字数 138 浏览 10 评论 0原文

我需要为自定义结构类型的 C 数组声明一个属性。

有人可以解释一下我应该如何处理这个 C 数组的属性声明吗?我根本不应该使用财产声明吗?

我尝试使用 memset() 处理数组中的内存,但由于编译错误而惨败。有人也可以解释一下吗?

I need to declare a property for a C array of a custom struct type.

Can someone explain what I should be doing with this C array as far as property declarations? Should I not be using a property declaration at all?

I tried memset()ing the memory in the array, and failed miserably with a compile error. Can someone explain that as well?

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

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

发布评论

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

评论(3

花间憩 2024-10-06 02:04:33

这个答案并不能真正解决你的问题,它只是解释了为什么你的方法不起作用。

在 C 中,不能从函数返回数组类型。这只是语言的限制(我不知道是有意还是无意)。推而广之,在 Objective-C 中你不能从方法返回数组类型。这也意味着您不能使用数组类型作为属性,因为编译器无法合成返回数组类型的方法。

C 数组在表达式中使用时会自动转换为指针类型,因此您可以遵循 @Graham 的建议并使用指针。这种方法的缺点是您必须确切地知道数组中有多少元素,这样您就不会意外地读到数组末尾。如果元素的数量是固定的并且在编译时已知,这可能是要走的路(如果你出于某种原因想避免 NSArray)。

在没有实际查看如何使用它的情况下,很难回答为什么 memset 无法编译,但一般来说,这就是它的使用方式:

MyStruct someStructs[10];

memset(someStructs, 0, sizeof someStructs);

或者,可以从函数/方法返回结构,即使它们包含数组。从理论上讲,您可以创建一个包含 MyStruct 数组的结构,并使用这个新的结构类型作为属性类型。实际上,这不是一个好主意,因为它只会增加一层复杂性。

This answer doesn't really solve your problem, it just explains why your approach didn't work.

In C you cannot return array types from functions. This is simply a limitation of the language (intentional or unintentional I don't know). By extension, in Objective-C you cannot return array types from methods. This also means that you can't use array types as properties, because the compiler is unable to synthesize a method that returns an array type.

C arrays are automatically converted into pointer types when used in an expression, so you can follow @Graham's advice and use pointers instead. The downside to this approach is that you must know exactly how many elements are in the array so that you don't accidentally read past the end of the it. If the number of elements is fixed and known at compile-time, this is probably the way to go (if you want to avoid NSArray for whatever reason).

It's difficult to answer whymemset failed to compile without actually seeing how you used it, but generally speaking this is how it can be used:

MyStruct someStructs[10];

memset(someStructs, 0, sizeof someStructs);

Alternatively, it is possible to return structs from functions/methods, even if they contain arrays. Theoretically speaking, you could create a struct containing an array of MyStruct and use this new struct type as the property type. Practically speaking this isn't a good idea since it just adds a layer of complexity.

兲鉂ぱ嘚淚 2024-10-06 02:04:33

如果您有这样的情况:

@interface MyClass
{
    struct MyStruct foo[5];
}
@end

您根本无法拥有读/写属性。您可以声明一个只读属性

@property (readonly, assign) struct MyStruct* fooProperty;

,它将为您提供指向数组的指针。然而,这可能很糟糕,因为它使代码的其余部分可以内部访问 MyClass 的结构。实现 KVC 索引访问器<​​/a>,您必须手动执行此操作。

If you have this:

@interface MyClass
{
    struct MyStruct foo[5];
}
@end

you can't have a read/write property at all. You can declare a read only property

@property (readonly, assign) struct MyStruct* fooProperty;

which will give you a pointer to the array. However, this is probably bad since it gives the rest of your code internal access to the structure of MyClass. It's proably better to implement the KVC indexed accessors, which you'll have to do manually.

我的痛♀有谁懂 2024-10-06 02:04:33

您当然可以将数组视为属性,它将具有“分配”行为,因为您无法保留结构。本质上,您仍然完全负责数组的内存分配。

您的另一个选择是用对象替换结构。然后它可以更自然地与属性机制一起工作,并向您开放像 NSArray 这样的 Foundation 集合类。

You certainly can treat the array as a property, it will have "assign" behaviour because you can't retain a struct. Essentially you're still totally responsible for the memory allocation of the array.

Another option you have is to replace your struct with an object. Then it works more naturally with the property machinery, and opens up the Foundation collection classes like NSArray to you.

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