Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 应为Xcode中的标识符或“(”_Objective C - Fatal编程技术网

Objective c 应为Xcode中的标识符或“(”

Objective c 应为Xcode中的标识符或“(”,objective-c,Objective C,这是我的第一个Mac应用程序,我正在用简单的代码出错 @interface AppDelegate : NSObject <NSApplicationDelegate> @property (assign) IBOutlet NSWindow *window; @property (assign) IBOutlet NSButton *AddHostsButton; NSFileManager *fileman; fileman = [NSFileManager defaultM

这是我的第一个Mac应用程序,我正在用简单的代码出错

@interface AppDelegate : NSObject <NSApplicationDelegate>
@property (assign) IBOutlet NSWindow *window;
@property (assign) IBOutlet NSButton *AddHostsButton;

NSFileManager *fileman;

fileman = [NSFileManager defaultManager];

if ([*fileman fileExistsAtPath:@"/private/etc/hosts" ] == YES)
NSLog @"Hosts exists";

else NSLog @"Hosts not found";


@end

在Obj-C中,字符串是这样写的,而不是那样写的


您需要将代码放在某个方法体中。

您的代码属于@implementation块而不是@interface块中,并且它需要是方法定义的一部分,而不仅仅是像您所示的那样挂在外面。它应该是这样的:

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    if ([[NSFileManager defaultManager] fileExistsAtPath:@"/private/etc/hosts"] == YES)
        NSLog (@"Hosts exists");
    else
        NSLog (@"Hosts not found");
    return YES;
}

@end

虽然Caleb和Cyrille已经确定了具体的问题,但总体问题是您没有编写Objective-C代码,这充其量只是伪代码

你需要从Objective-C的初学者指南开始——有很多——然后学习这门语言


这并不是侮辱-我们都曾有过这样的经历!

希望在添加@之后返回Objective-C指令。咳嗽您需要给我更多信息,noob这里。[NSFileManager fileExistsAtPath:@/private/etc/hosts]-这就是你所拥有的吗?去掉字符串周围的括号。Objective-c用括号命名了参数,因此你不需要括号。我没有使用NSFileManager*fileman,而是使用了该变量,但现在出现了我提到的大量错误。类方法“+fileExistsAtPath not found”返回类型默认为“id”@user1222053我从未使用过med您的代码是正确的-我只是把它转换成一种至少应该有一定意义的形式。至于那个特定的错误,是因为fileExistsAtPath:是一个实例方法而不是类方法。我将在上面的代码中解决这个问题,但您确实应该学习更多的语言并学习使用文档。有新事物没有什么错,但这不应该阻止你找出许多错误。