gpt4 book ai didi

swift/ cocoa : launchApplication(at:options:configuration:) and catching errors

转载 作者:行者123 更新时间:2023-11-28 11:34:38 25 4
gpt4 key购买 nike

所以...我想使用此功能在给定的 URL 启动应用程序。问题是,开发人员文档说:

Parameters

url

The application URL.

options

Options to use when launching the application. SeeNSWorkspace.LaunchOptions for possible values.

configuration

A dictionary containing the configuration options. Thisdictionary can be used to pass additional options to the app. Possiblekey-value pairs are described in Workspace Launch ConfigurationOptions. The configuration dictionary may be empty, in which casedefault behavior applies.

error

Returns, by-reference, the error if the application was unableto be launched. You may specify nil for this parameter if you do notwant the error information.

Return Value

If the app is already running, and newInstance is notspecified in the options dictionary, then a reference to the existingapp is returned; otherwise a new application reference is returned. Ifthe application could not be launched, nil is returned and the erroris specified in error.

问题是,这与函数签名根本不匹配,即:

func launchApplication(at url: URL, options: NSWorkspace.LaunchOptions = [], configuration: [NSWorkspace.LaunchConfigurationKey : Any]) throws -> NSRunningApplication

我到底应该在哪里指定所述错误?

最佳答案

除错误处理外,文档中提到的参数与实际方法完全一致。两者都有 urloptionsconfiguration。不过,由于 Swift 具有参数标签的能力,您可以在实际调用方法时将 aturl 参数一起使用。

大多数具有 NSError **error 最后参数的 Objective-C API 在 Swift 中被转换为没有错误参数的 API。相反,他们声明他们throw

如果您阅读了您引用的方法的其余文档,您将看到:

Handling Errors In Swift:

In Swift, this method returns a nonoptional result and is marked with the throws keyword to indicate that it throws an error in cases of failure.

You call this method in a try expression and handle any errors in the catch clauses of a do statement, as described in Error Handling in The Swift Programming Language and About Imported Cocoa Error Parameters.

您按如下方式处理错误:

do {
// pass real options and configuration as needed
try someWorkspace.launcApplication(at: someURL, options: [], configuration: [:])
} catch {
// handle error here as needed
print(error)
}

关于 swift/ cocoa : launchApplication(at:options:configuration:) and catching errors,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55853883/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com