【问题标题】:Why does the compiler say I haven't declared my variable?为什么编译器说我没有声明我的变量?
【发布时间】:2009-12-13 19:41:44
【问题描述】:

我在 Xcode 中收到一个错误,提示“whatEverVariable 未声明”这发生在 .m 文件内部以及类本身之外。我已经在 .h 文件的花括号内声明了该变量,并且正在使用 @property 和 @synthesize 来访问该变量。但是,我有时会在 .m 文件中遇到上述错误,甚至在我 #imported 包含该变量的类的其他类中也是如此。我在这里总体上是否遗漏了有关 Objective-C 的一些东西或什么?我想我要问的是,如果它们被声明,为什么会不被声明。

我还收到“按钮可能无法响应“setTitle”作为错误,这已被弃用或“setTitle:forState:”,因为我不想使用它。

我会发布一个示例,但我真的认为我在这里遗漏了一些明显的东西。有人对导致错误的原因有任何想法吗?抱歉这么笼统,但我必须是个白痴或什么的?

这是说“picturesInArray 未声明”

#import <Foundation/Foundation.h> 
@interface Pictures : NSObject { 
    NSMutableArray *picturesInArray; 
} 

@property (nonatomic, retain) NSMutableArray *picturesInArray; 

-(IBAction)pictureButtonPressed:(id)sender; 

@end 


#import "Pictures.h" 
@implementation Pictures 

@synthesize picturesInArray; 

-(IBAction)pictureButtonPressed:(id)sender { 

    if (pictureIndex >= [picturesInArray count] || pictureIndex < 0) { 
        pictureIndex = 0;    

        [imageView setImage:[picturesInArray objectAtIndex:pictureIndex]]; 

        [imageView setNeedsDisplay]; 
    }    
}

【问题讨论】:

  • 我确信您遗漏了一些明显的东西,但是如果没有看到任何代码,就很难准确判断您犯了什么错误。
  • 您还应该在标题中发布问题摘要。你能想象登录 SO 并且首页有 50 个条目都说“我有一个问题。”?
  • 你一定错过了实现块末尾的@end。显然,您没有显示整个代码。能给我们提供这个类的完整代码吗?或者,否则,去除所有冗余变量的当前样本,并尝试仅使用定义的 picturesInArray 对其进行编译。

标签: objective-c cocoa


【解决方案1】:

这是说“picturesInArray 未声明”

您确定不是说“pictureIndex 未声明”吗?因为那是您显示的代码中未声明的变量。

【讨论】:

    【解决方案2】:

    您可以发布示例代码吗?

    我的猜测是,您在某些时候没有声明 whatEverVariable 的对象/变量类型。确保您的标题如下所示:

    MyClass.h:

    @interface MyClass : NSObject 
    {
        NSString* whatEverVariable;
    }
    
    @property(readwrite, assign) NSString* whatEverVariable;
    
    @end
    

    MyClass.m:

    @implementation MyClass
    
    //whatever methods
    
    @synthesize whatEverVariable;
    
    @end
    

    请注意,您在 @property 中再次声明了变量的类型。

    对于按钮,请确保它是 NSButton 类型。

    另外,你进口了吗?这会为 NSButton 之类的东西导入很多标头。

    【讨论】:

    • 这是说“picturesInArray 未声明”#import @interface Pictures : NSObject { NSMutableArray *picturesInArray; } @property (nonatomic, 保留) NSMutableArray *picturesInArray; -(IBAction)pictureButtonPressed:(id)sender; @end #import "Pictures.h" @implementation Pictures @synthesize picturesInArray; -(IBAction)pictureButtonPressed:(id)sender { if (pictureIndex >= [picturesInArray count] || pictureIndex
    【解决方案3】:

    这是说“picturesInArray 未声明”很抱歉把它放在上面的“cmets 使它难以阅读!”

    #import <Foundation/Foundation.h> 
    @interface Pictures : NSObject { 
        NSMutableArray *picturesInArray; 
    } 
    
    @property (nonatomic, retain) NSMutableArray *picturesInArray; 
    
    -(IBAction)pictureButtonPressed:(id)sender; 
    
    @end 
    
    
    #import "Pictures.h" 
    @implementation Pictures 
    
    @synthesize picturesInArray; 
    
    -(IBAction)pictureButtonPressed:(id)sender { 
    
        if (pictureIndex >= [picturesInArray count] || pictureIndex < 0) { 
            pictureIndex = 0;    
    
            [imageView setImage:[picturesInArray objectAtIndex:pictureIndex]]; 
    
            [imageView setNeedsDisplay]; 
        }    
    }
    

    【讨论】:

    • 这对我来说很好。如果我添加“int pictureIndex; NSImageView *imageView;”到 .h 文件,这对我来说很好,没有错误或警告。你用的是什么编译器?
    猜你喜欢
    • 2023-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多