变量“未声明”仅当为设备编译时

发布于 2024-11-09 17:08:06 字数 2592 浏览 0 评论 0原文

我收到错误“currentUpdateMethod”未声明(在此函数中首次使用)。它引用的变量 currentUpdateMethod 是头文件中声明的 SEL 类型的实例变量。因为构建模拟器并运行应用程序按预期工作,所以我相信我已经正确设置了所有内容。这个错误今天才出现 - 我已经在设备上测试了几天,没有出现问题。我确实尝试过清理和清理所有目标。我什至在 xcode 中将变量名称输入到文件中,它会自动为我完成变量。什么可能导致设备编译在这些变量上失败,但模拟器编译却失败?

编辑:代码如下。

超类:

#import "Deployable.h"

@interface Drawable : Deployable {

float currentDelta;

SEL currentUpdateMethod;
SEL currentAnimationMethod;
SEL currentBatchMethod;

float rotation;
}

- (id) init;

- (id) initWithActivationTime:(float)time;

- (int) updateWithDelta:(float)delta;

- (int) animate;

- (int) batch;

@end

然后是问题类:

#import "Drawable.h"
#import "Structures.h" //contains Vector2f declaration

@interface Player : Drawable {

    Image *playerGraphic;

    Vector2f position;

}

@property (nonatomic) Vector2f position;

- (id) initWithImage:(Image *)aGraphic andPosition:(Vector2f)aPosition;

- (void) setupInactiveState;
- (int) updateInactiveState;
- (int) animateInactiveState;
- (int) batchInactiveState;

- (void) setupActiveState;
- (int) updateActiveState;
- (int) animateActiveState;
- (int) batchActiveState;

@end

及其实现,其中抛出错误:

#import "Player.h"
#import "AIEngine.h"

@implementation Player

@synthesize position;

- (id) initWithImage:(Image *)aGraphic andPosition:(Vector2f)aPosition {

    self = [super init];

    if(self) {

        playerGraphic = [aGraphic retain];
        position = aPosition;


    }

    return self;
}

- (int) deployWithScene:(MainScene *)newScene {

    [super deployWithScene:newScene];

    [self setupInactiveState];

    return 1;
}

- (void) setupInactiveState {

    currentUpdateMethod = @selector(updateInactiveState); //'currentUpdateMethod' undeclared (first use in this function)
    currentAnimationMethod = @selector(animateInactiveState); //'currentAnimateMethod' undeclared (first use in this function)
    currentBatchMethod = @selector(batchInactiveState); //'currentAnimateMethod' undeclared (first use in this function)

}

- (void) setupActiveState {    

    currentUpdateMethod = @selector(updateActiveState); //'currentUpdateMethod' undeclared (first use in this function)
    currentAnimationMethod = @selector(animateActiveState); //'currentAnimateMethod' undeclared (first use in this function)
    currentBatchMethod = @selector(batchActiveState); //'currentBatchMethod' undeclared (first use in this function)

}

@end

重申一下,这六个错误在构建设备时抛出。当我为模拟器构建时,应用程序会正常构建并运行。

Edit2:我只切换到 LLVM 并且没有抛出错误。我想找出问题的根源,而不是仅仅使用其他编译器。有什么想法吗?

I'm receiving the error 'currentUpdateMethod' undeclared (first use in this function). The variable this refers to, currentUpdateMethod, is an instance variable of type SEL declared in the header file. Because building to the simulator and running the app works as expected, I believe I have everything set up correctly. This error only appeared today - I have been testing on the device for several days now without issue. I did attempt to clean and clean all targets. I even type out the variable name into the file in xcode and it autocompletes the variable for me. What could cause the device compile to fail on these variables but not the compile for simulator?

Edit: Code follows.

Superclass:

#import "Deployable.h"

@interface Drawable : Deployable {

float currentDelta;

SEL currentUpdateMethod;
SEL currentAnimationMethod;
SEL currentBatchMethod;

float rotation;
}

- (id) init;

- (id) initWithActivationTime:(float)time;

- (int) updateWithDelta:(float)delta;

- (int) animate;

- (int) batch;

@end

Then the problem class:

#import "Drawable.h"
#import "Structures.h" //contains Vector2f declaration

@interface Player : Drawable {

    Image *playerGraphic;

    Vector2f position;

}

@property (nonatomic) Vector2f position;

- (id) initWithImage:(Image *)aGraphic andPosition:(Vector2f)aPosition;

- (void) setupInactiveState;
- (int) updateInactiveState;
- (int) animateInactiveState;
- (int) batchInactiveState;

- (void) setupActiveState;
- (int) updateActiveState;
- (int) animateActiveState;
- (int) batchActiveState;

@end

And its implementation, where the errors are thrown:

#import "Player.h"
#import "AIEngine.h"

@implementation Player

@synthesize position;

- (id) initWithImage:(Image *)aGraphic andPosition:(Vector2f)aPosition {

    self = [super init];

    if(self) {

        playerGraphic = [aGraphic retain];
        position = aPosition;


    }

    return self;
}

- (int) deployWithScene:(MainScene *)newScene {

    [super deployWithScene:newScene];

    [self setupInactiveState];

    return 1;
}

- (void) setupInactiveState {

    currentUpdateMethod = @selector(updateInactiveState); //'currentUpdateMethod' undeclared (first use in this function)
    currentAnimationMethod = @selector(animateInactiveState); //'currentAnimateMethod' undeclared (first use in this function)
    currentBatchMethod = @selector(batchInactiveState); //'currentAnimateMethod' undeclared (first use in this function)

}

- (void) setupActiveState {    

    currentUpdateMethod = @selector(updateActiveState); //'currentUpdateMethod' undeclared (first use in this function)
    currentAnimationMethod = @selector(animateActiveState); //'currentAnimateMethod' undeclared (first use in this function)
    currentBatchMethod = @selector(batchActiveState); //'currentBatchMethod' undeclared (first use in this function)

}

@end

Just to reiterate, those six errors are only thrown when building for device. When I build for simulator, the app builds and runs normally.

Edit2: I switched to LLVM only and the errors were not thrown. I'd like to figure out the source of the problem instead of just using the other compiler. Any ideas?

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

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

发布评论

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

评论(2

我为君王 2024-11-16 17:08:06

我非常确定这是 Xcode 中的一个错误,因为我可以看到您的代码没有任何问题。

我会尝试以下两件事:

1)为了快速修复,您可以尝试综合变量,然后替换

currentUpdateMethod = @selector(updateInactiveState);

[self setCurrentUpdateMethod:@selector(updateInactiveState)];

2)从项目中删除文件。从头开始重新创建该类。将旧代码复制到新文件中。

看起来这个人也有类似的问题: 关于实例变量和变量的奇怪错误superclass

让我知道这些建议是否有帮助。如果他们这样做,我恳请您将错误提交给 Xcode 开发人员:)

I am pretty certain that this is a bug in Xcode, as there is nothing wrong with your code that I can see.

I would try these two things:

1) For a quick fix, you could try synthesizing your variables and then replacing

currentUpdateMethod = @selector(updateInactiveState);

with

[self setCurrentUpdateMethod:@selector(updateInactiveState)];

2) Remove the files from your project. Create the class again from scratch. Copy the old code into the new file.

Looks like this guy had a similar problem: Strange error regarding instance variables & superclass

Let me know if these suggestions help. If they do, I would kindly ask you to submit the bug to the Xcode devs :)

雅心素梦 2024-11-16 17:08:06

我几周来都遇到同样的问题

编译到 iOS 设备时出现“变量未声明”错误,但不适用于模拟器

我发现的一种解决方案是简单地将编译器从默认的 LLVM GCC 4.2 更改为LLVM 编译器 2.0(或“Apple LLVM 编译器 2.1”)。这似乎是编译器中的一个错误,但这只是猜测。

如果您根本不需要使用 GCC 编译器,则更改它可以快速解决您的问题。

I am having the same issue for weeks

"Variable Undeclared" error when compiling to iOS Device, but not for Simulator

One solution I found is to simply change the compiler from the default LLVM GCC 4.2 to LLVM Compiler 2.0 (or to `Apple LLVM Compiler 2.1). It seems to be a bug in the compiler, but it is just a guess.

Changing it is a quick fix for your problem if there is no need for you to use the GCC compiler at all.

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