这个示例代码让我定义了一些东西,但我还不想定义?

发布于 2024-11-05 07:24:44 字数 777 浏览 0 评论 0原文

我有一些开源代码,其中包括:

.h:

#define TILE_ROWS    6
#define TILE_COLUMNS 2
#define TILE_COUNT   (TILE_ROWS * TILE_COLUMNS)

@class Tile;

@interface TilesViewController : UIViewController {
@private
    CGRect   tileFrame[TILE_COUNT];
    Tile    *tileForFrame[TILE_COUNT];

}

然后在整个 .m 中,如下所示:

   for (int row = 0; row < TILE_ROWS; ++row) {
        for (int col = 0; col < TILE_COLUMNS; ++col) {

and

tileFrame[index] = frame;

and

 tileForFrame[index] = tile;

但我想要的是能够将 TILE_ROWS 设置为结果,例如:

float rowsNeeded = ceil(rowsNeededA/TILE_COLUMNS);

因此,它需要稍后再说,但我认为 CGRect 和 Tile 只能在那里定义。我需要帮助,我不知道该怎么办。

I have some open source code, which includes this:

.h:

#define TILE_ROWS    6
#define TILE_COLUMNS 2
#define TILE_COUNT   (TILE_ROWS * TILE_COLUMNS)

@class Tile;

@interface TilesViewController : UIViewController {
@private
    CGRect   tileFrame[TILE_COUNT];
    Tile    *tileForFrame[TILE_COUNT];

}

And then throughout the .m, like so:

   for (int row = 0; row < TILE_ROWS; ++row) {
        for (int col = 0; col < TILE_COLUMNS; ++col) {

and

tileFrame[index] = frame;

and

 tileForFrame[index] = tile;

But what i want is to be able to set TILE_ROWS to the outcome of, for example:

float rowsNeeded = ceil(rowsNeededA/TILE_COLUMNS);

So therefore it would need to be later on, but I think the CGRect and Tile can only be defined there. I need help, I'm not sure what to do.

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

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

发布评论

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

评论(1

站稳脚跟 2024-11-12 07:24:44

您必须将定义转换为实例变量并动态分配数组:

@class Tile;

@interface TilesViewController : UIViewController {
@private
    int       tileRows;
    int       tileColumns;
    int       tileCount;
    CGRect*   tileFrame;
    Tile**    tileForFrame;
}

...

tileColumns = 2;
tileRows = (rowsNeededA + tileColumns - 1) / tileColumns;
tileCount = tileColumns * tileRows;
tileFrame = (CGRect*)malloc(tileCount * sizeof(CGRect));
tileForFrame = (Tile**)malloc(tileCount * sizeof(Tile*));

for 循环变为:

for (int row = 0; row < tileCount; ++row) {
    for (int col = 0; col < tileCount; ++col) {

You have to turn the defines into instance variable and to allocate the arrays dynamically:

@class Tile;

@interface TilesViewController : UIViewController {
@private
    int       tileRows;
    int       tileColumns;
    int       tileCount;
    CGRect*   tileFrame;
    Tile**    tileForFrame;
}

...

tileColumns = 2;
tileRows = (rowsNeededA + tileColumns - 1) / tileColumns;
tileCount = tileColumns * tileRows;
tileFrame = (CGRect*)malloc(tileCount * sizeof(CGRect));
tileForFrame = (Tile**)malloc(tileCount * sizeof(Tile*));

The for loop then becomes:

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