Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.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 使用目标c中的完成处理程序调用Swift函数_Objective C_Swift_Asynchronous_Callback - Fatal编程技术网

Objective c 使用目标c中的完成处理程序调用Swift函数

Objective c 使用目标c中的完成处理程序调用Swift函数,objective-c,swift,asynchronous,callback,Objective C,Swift,Asynchronous,Callback,我试图调用一个Swift函数,该函数在一个目标C类中包含一个完成处理程序,但我不确定如何实现它 这是我的Swift密码 @objc class textToSpeech:NSObject{ func toSpeech(word: NSString, sucess:()->Void) -> NSURL { let tempDirectory = NSURL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)

我试图调用一个Swift函数,该函数在一个目标C类中包含一个完成处理程序,但我不确定如何实现它

这是我的Swift密码

@objc class textToSpeech:NSObject{

func toSpeech(word: NSString, sucess:()->Void) -> NSURL {
    let tempDirectory = NSURL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
    let tempFile = tempDirectory.URLByAppendingPathComponent((word as String) + ".wav")

    let tts = TextToSpeech(username: "xxxxxx", password: "xxxxxx")
    tts.synthesize(word as String,
               voice: SynthesisVoice.GB_Kate,
               audioFormat: AudioFormat.WAV,
               failure: { error in
                print("error was generated \(error)")
}) { data in

        data.writeToURL(tempFile, atomically: true)
        print("createdURL")
        print(tempFile)
        sucess();

}

return tempFile
}

如何在目标c中编写函数调用。我已经完成了项目设置,以便可以从目标c调用swift函数。

我想应该是这样的:

textToSpeech* text = [[textToSpeech alloc] init];

[text word:@"some text" sucess:^{
    NSLog(@"success");
}];

例如,您有以下代码:

@objc class PDTextToSpeech: NSObject{
  func toSpeech(word: NSString, success: () -> Void) -> NSURL {
    // ...
    return NSURL()
  }
}
因此,您可以轻松地将obj-c中的Swift代码与
#import“-Swift.h”连接起来
项目名称所在的位置

然后你可以打电话:

[[PDTextToSpeech new] toSpeech:@"String" success:^{
    NSLog(@"Success");
}];
我使用了
PDTextToSpeech
作为类名,因为最好用uniq前缀调用obj-c中的类。如果项目名为
TestProject
,则可以使用
TP
前缀