【问题标题】:Variable "undeclared" only when compiling for device仅在为设备编译时变量“未声明”
【发布时间】:2011-05-25 05:15:55
【问题描述】:

我收到未声明的错误“currentUpdateMethod”(在此函数中首次使用)。 this 引用的变量 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 并且没有抛出错误。我想找出问题的根源,而不仅仅是使用其他编译器。有什么想法吗?

【问题讨论】:

  • 你能给我们看看代码吗?您可能在某处有一些 #ifdef 宏可能会搞砸。
  • @BoltClock 你真的认为他会知道如何放置这样的宏 - 但没有意识到这是导致问题的原因?但我确实同意,没有代码,我们无能为力...
  • 您使用的是 GCC 还是 LLVM? GCC 有一些...与“受保护”变量相关的有趣错误(请参阅stackoverflow.com/questions/2778405/…
  • 如果不是 LLVM,我会尝试切换编译器。奇怪的是它一次编译没有错误,而且最近才开始。

标签: iphone objective-c ios compilation device


【解决方案1】:

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

我会尝试这两件事:

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

currentUpdateMethod = @selector(updateInactiveState);

[self setCurrentUpdateMethod:@selector(updateInactiveState)];

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

看起来这个人也有类似的问题:Strange error regarding instance variables & superclass

如果这些建议有帮助,请告诉我。如果他们这样做了,我会请您将错误提交给 Xcode 开发人员:)

【讨论】:

    【解决方案2】:

    我连续几周都遇到同样的问题

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

    我发现的一个解决方案是将编译器从默认的LLVM GCC 4.2 更改为LLVM Compiler 2.0(或“Apple LLVM Compiler 2.1”)。貌似是编译器的bug,不过只是猜测而已。

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

    【讨论】:

    • 这似乎是一个编译器错误。在我的帖子末尾,我提到切换到 LLVM,错误就消失了。
    • 对不起,没有看到编辑...所以你的问题和我的一模一样
    • 确实如此。正如 3rdFunkyBot 所建议的那样,通过消息传递显式设置变量确实修复了“错误”。
    猜你喜欢
    • 2017-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-28
    • 1970-01-01
    • 1970-01-01
    • 2017-06-26
    相关资源
    最近更新 更多