IOS/Objective-C/Swift/Speech: 在声明 SFSpeech 识别器变量时指定语言环境
IOS/Objective-C/Swift/Speech: Specify Locale when declaring SFSpeech Recognizer variable
我正在尝试将我刚刚学习的一些 Swift 翻译成 Objective-C 用于语音项目。
Swift 显然允许您在声明变量时指定 speechRecognizer 的语言环境,如下所示:
private let speechRecognizer = SFSpeechRecognizer(locale: Locale.init(identifier: "en-US"))
是否可以在 Objective-C 中执行此操作?现在我在接口中声明了一个变量:
SFSpeechRecognizer *speechRecognizer;
稍后再设置语言环境:
speechRecognizer = [[SFSpeechRecognizer alloc] initWithLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en-US"]];
理想情况下,我想在声明的一开始就这样做,但我不清楚 Swift 和 Objective-C 之间的区别。
感谢您的任何建议或见解。
考虑按以下顺序构建的 Swift 调用:
// Create a Locale object for US English
let locale = Locale.init(identifier: "en-US")
// Create a speech recognizer object for US English
let speechRecognizer = SFSpeechRecognizer(locale: locale)
然后将 Swift 代码与 Objective-C 进行比较:
// Here you are create an uninitialized variable of type SFSpeechRecognizer
// this will then hold the SFSpeechRecognizer when you initialize it in the next line
SFSpeechRecognizer *speechRecognizer;
// This is accomplishing the same logic as the above Swift call
speechRecognizer = [[SFSpeechRecognizer alloc] initWithLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en-US"]];
如果您希望将 objective-c 调用写成一行,您可以将其重写为如下所示:
SFSpeechRecognizer *speechRecognizer = [[SFSpeechRecognizer alloc] initWithLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en-US"]];
这两种方法都没有错,只是Swift可以推断变量类型,所以在启动语音识别器之前不需要创建空变量。 Objective-C 可以 NOT 推断变量类型,因此命令可能被拆分只是为了让行更短一些。
我正在尝试将我刚刚学习的一些 Swift 翻译成 Objective-C 用于语音项目。
Swift 显然允许您在声明变量时指定 speechRecognizer 的语言环境,如下所示:
private let speechRecognizer = SFSpeechRecognizer(locale: Locale.init(identifier: "en-US"))
是否可以在 Objective-C 中执行此操作?现在我在接口中声明了一个变量:
SFSpeechRecognizer *speechRecognizer;
稍后再设置语言环境:
speechRecognizer = [[SFSpeechRecognizer alloc] initWithLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en-US"]];
理想情况下,我想在声明的一开始就这样做,但我不清楚 Swift 和 Objective-C 之间的区别。
感谢您的任何建议或见解。
考虑按以下顺序构建的 Swift 调用:
// Create a Locale object for US English
let locale = Locale.init(identifier: "en-US")
// Create a speech recognizer object for US English
let speechRecognizer = SFSpeechRecognizer(locale: locale)
然后将 Swift 代码与 Objective-C 进行比较:
// Here you are create an uninitialized variable of type SFSpeechRecognizer
// this will then hold the SFSpeechRecognizer when you initialize it in the next line
SFSpeechRecognizer *speechRecognizer;
// This is accomplishing the same logic as the above Swift call
speechRecognizer = [[SFSpeechRecognizer alloc] initWithLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en-US"]];
如果您希望将 objective-c 调用写成一行,您可以将其重写为如下所示:
SFSpeechRecognizer *speechRecognizer = [[SFSpeechRecognizer alloc] initWithLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en-US"]];
这两种方法都没有错,只是Swift可以推断变量类型,所以在启动语音识别器之前不需要创建空变量。 Objective-C 可以 NOT 推断变量类型,因此命令可能被拆分只是为了让行更短一些。