声明一个带有多个参数的 Objective-C 方法

Mik*_*e97 2 methods syntax objective-c

我想将 NSTask(NSTask、launchpath 和 args)传递给一个函数,但我确实需要一些语义方面的帮助:

AppleDelegate.h 文件:

- (void)doTask:(NSTask*)theTask :(NSArray*)arguments :(NSString*)launchPath;
Run Code Online (Sandbox Code Playgroud)

AppleDelegate.m 文件:

- (void)doTask:(NSTask*)theTask :(NSArray*)arguments :(NSString*)launchPath
{
    self.currentTask = theTask;

    // do currentTask alloc, set the launcpath and the args, pipe and more
}
Run Code Online (Sandbox Code Playgroud)

这是调用“doTask”的代码:

NSTask* runMyTask;
NSString *command = @"/usr/bin/hdiutil";
NSArray* taskArgs = [NSArray arrayWithObjects:@"info", @"/", nil];

// Here the error:
[self doTask:runMyTask, taskArgs, command];  // ARC Semantic issue no visible interface for AppleDelegate declares the selector 'doTask'..
Run Code Online (Sandbox Code Playgroud)

选择器显示为未声明,但我以为我确实声明了它......是否可以做这样的事情,错误在哪里?

Joh*_*rug 6

你应该先做一个这样的教程来了解 Objective-C 的基础知识:http : //tryobjectivec.codeschool.com/

关于你的问题。你的方法被调用了doTask:::。你可以调用它是这样的:[self doTask:runMyTask :taskArgs :command];。然而,这是不好的做法。您希望每个参数都反映在您的方法名称中。此外,您不希望方法名称以do或开头does

因此,重命名您的方法:

- (void)runTask:(NSTask *)theTask arguments:(NSArray *)arguments launchPath:(NSString *)launchPath;
Run Code Online (Sandbox Code Playgroud)

并这样称呼它:

[self runTask:runMyTask arguments:taskArgs launchPath:command];
Run Code Online (Sandbox Code Playgroud)

完毕。