문제

I have a applescript-objc script with a method as follows :-

on say_(phrase, userName)
    set whatToSay to "\"" & phrase & " " & userName & "\""
    say whatToSay
end say_

And i want to call this method from objective-c but cant seem to figure out how to call methods with multiple arguments, i have no problem calling methods with only one argument as follows :-

@interface NSObject (ASHandlers)
- (void)say:(NSString *)phrase;
@end

@implementation AppDelegate

@synthesize window, sayTextField;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification{
   scriptFile = NSClassFromString(@"Test");
   if ( !scriptFile ){
      // Handle errors here
   return;
   }
}

- (IBAction)say:(id)sender{
   NSString *phrase = [sayTextField stringValue];
   [scriptFile say:phrase];
}

please can someone help.

Regards, Andy.

도움이 되었습니까?

해결책

For starters, IBActions must have a signature of:

-(void)action;
-(void)actionWithSender:(id)sender;
-(void)actionWithSender:(id)sender event:(UIEvent*)event;

So you can't have an IBAction with multiple arguments if that's what you were looking for.

However, to answer your question, to have a method with multiple arguments, in Objective-C, it would look like this:

- say:(NSString *)textToSay withUserName:(NSString *)userName {
   ...
}

In AppleScriptObjC, you move all of your Objective-C method parameters to the beginning of the method name, replace the colons with underscores, and put your arguments in the parentheses.

on say_withUserName_(textToSay, userName)
    ...
end say_withUserName_ 
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top