Xcode拒绝承认编写良好的代码

时间:2012-04-10 06:46:53

标签: objective-c xcode compiler-errors

  

可能重复:
  error: expected specifier-qualifier-list before…in Objective C?

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

Xcode refuses to acknowledge 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

HelpCommand.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

HelpCommand.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

1 个答案:

答案 0 :(得分: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