gpt4 book ai didi

objective-c - Xcode拒绝确认编写良好的代码

转载 作者:行者123 更新时间:2023-12-02 11:15:37 25 4
gpt4 key购买 nike

这个问题已经在这里有了答案:




已关闭8年。




Possible Duplicate:
error: expected specifier-qualifier-list before…in Objective C?



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

类(class)

我的世界
//
// 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

最佳答案

除非绝对需要 header ,否则使用@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

关于objective-c - Xcode拒绝确认编写良好的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10084296/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com