-5

可能重复:
错误:预期的说明符限定符列表之前......在Objective C中?

老实说,我不知道这里出了什么问题。这段代码以前工作过,所有相关文件都写得很好,可以自己编译(甚至是各自的.m文件),但是我.h的一个 OC 项目中有一个文件,其中 Xcode 不断抛出相同的编译错误。有时每行多次,有时只有一次,但总是在useCommand变量上。下面是整个受影响.h文件的屏幕截图和复制/粘贴代码,这是唯一无法编译的文件。当我注释掉对 的每个引用时,程序运行完美useCommand,但是当我取消注释它们时,再次发生这种情况。我什至创建了一个新项目并将所有代码复制/粘贴到新文件中,但仍然出现此错误。当我移动useCommand引用新行,Xcode 的错误随之而来。有人知道 Xcode 发生了什么吗?我很确定这不是我的错误,因为我已经四次检查了我的代码以确保一切正常,并且我已经多次清理了目标。

Xcode 拒绝承认 useCommand

课程


Minecraftia.h

//
//  Minecraftia.h
//  TextCraft
//
//  Created by Supuhstar on 4/3/12.
//  Copyright 2012 Blue Husky Programming. All rights reserved.
//

#import <Cocoa/Cocoa.h>
#import "Command.h"
#import "HelpCommand.h"
#import "UseCommand.h"
#import "GetCommand.h"
#import "LookCommand.h"
#import "IO.h"

@interface Minecraftia : NSObject
{
    HelpCommand *helpCommand;
    UseCommand *useCommand;
    GetCommand *getCommand;
    LookCommand *lookCommand;
}
-(id)init;

/**
 * If none has already been created, creates a static instance of Minecraftia
 * 
 * Returns the same instance of Minecraftia each time
 */
+(Minecraftia *)sharedInstance;

/**
 * Turns the given string into a command
 * If no matching command is found, nil is returned
 */
-(Command *)toCommand:(NSString *)input;

/**
 * The main method of the game, wherein all interactions happen
 */
-(void)play;

/**
 * Returns a random message to be used as splash text when the program is started
 */
-(NSString *)getASplash;

/**
 * Returns an NSArray of all the available commands
 */
-(NSArray*)getRegisteredCommands;

@property (retain, nonatomic, readonly) HelpCommand *helpCommand;
@property (retain, nonatomic, readonly) UseCommand *useCommand;
@property (retain, nonatomic, readonly) GetCommand *getCommand;
@property (retain, nonatomic, readonly) LookCommand *lookCommand;
@end

帮助命令.h

//
//  HelpCommand.h
//  TextCraft
//
//  Created by Student4 on 4/9/12.
//  Copyright 2012 Blue Husky Programming. All rights reserved.
//

#import <Cocoa/Cocoa.h>
#import "Command.h"
#import "Minecraftia.h"

@interface HelpCommand : Command {

}

@end

帮助命令.m

//
//  HelpCommand.m
//  TextCraft
//
//  Created by Student4 on 4/9/12.
//  Copyright 2012 Blue Husky Programming. All rights reserved.
//

#import "HelpCommand.h"


@implementation HelpCommand
-(bool)execute:(NSArray *)info
{
    NSString *helpString = @"Here are all the available commands:\n";
    NSArray *commands = [[Minecraftia sharedInstance] getRegisteredCommands];
    for(int i=0, l=[commands count]; i < l; i++)
    {
        helpString = [NSString stringWithFormat:@"\t> %@", [helpString stringByAppendingString:[[[commands objectAtIndex:i] class] triggerText]]];
    }
    MyLog([NSString stringWithFormat:@"%@\n\n", helpString]);
    return true;
}



+(NSString *)triggerText
{
    static NSString *triggerText = @"HELP";
    return triggerText;
}
@end
4

1 回答 1

2

除非您绝对需要标头,否则请使用 @class 而不是 import,然后将导入移动到您的 .m 文件中。现在您将编译器置于一个循环中,Minecraftia.h 正在导入 UseCommand.h,而 UseCommand.h 正在导入 Minecraftia.h。

应该是这样的:

#import <Cocoa/Cocoa.h>
#import "IO.h"

@class Command, HelpCommand, UseCommand, GetCommand, LookCommmand;

@interface Minecraftia : NSObject
{
    HelpCommand *helpCommand;
    UseCommand *useCommand;
    GetCommand *getCommand;
    LookCommand *lookCommand;
}

/* .... */

@end
于 2012-04-10T12:59:16.250 回答