简体   繁体   English

如何在结构定义中定义CGPoints数组?

[英]How to define array of CGPoints within a struct definition?

I want to do this: 我想做这个:

typedef struct
{
    CGPoint vertices[];
    NSUInteger vertexCount;
} Polygon;

But it says Field has incomplete type CGPoint [] . 但它说Field has incomplete type CGPoint []

You need to do one of two things: 你需要做两件事之一:

  1. Declare the array to be a fixed size (probably not what you want) 声明数组是固定大小(可能不是你想要的)
  2. Make it a pointer. 把它变成指针。 But then you need to properly malloc and free the array as needed. 但是你需要正确地malloc并根据需要free数组。

A better choice is to not use a struct and instead create a full class. 更好的选择是不使用struct而是创建完整的类。 Then you can add methods and properties as well as make memory management much easier. 然后,您可以添加方法和属性,以及使内存管理更容易。 You are working in Objective-C. 您正在使用Objective-C。 Take advantage of the Object Oriented aspects of the language. 利用该语言的面向对象方面。 Add a method to calculate the circumference and area, etc. Put the logic where it belongs. 添加一种方法来计算周长和面积等。将逻辑放在它所属的位置。

设置数组大小CGPoint vertices[count];

Don't you want a unique name for each element of your struct anyway? 您是否想要为结构的每个元素使用唯一的名称? If you just want a bunch of CGPoint's in a numerical order, with the ability to count how many of them there are you'd be much better served by shoving them in an NSArray or NSMutableArray (stored as NSValue's of course) 如果你只是想要一个CGPoint的数字顺序,能够计算它们中的多少,你可以通过在NSArray或NSMutableArray(当然存储为NSValue)中推送它们来获得更好的服务

The whole point of a struct would be to have easy access to the values by a descriptive name, ie: 结构的重点是通过描述性名称轻松访问值,即:

typedef struct {
    CGPoint helpfulAndDescriptiveNameOne;
    CGPoint helpfulAndDescriptiveNameTwoWhichIsDifferentThanTheOtherName;
    etc...
    NSUInteger vertexCount;
}

For example, a CGRect is just a struct composed of four different CGFloats, each of which is descriptively and helpfully named: 例如,CGRect只是一个由四个不同的CGFloats组成的结构,每个CGFloats都具有描述性和有用的命名:

typedef {
    CGFloat x;
    CGFloat y;
    CGFloat width;
    CGFloat height;
} CGRect;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM