NSRunningApplication - 终止
我将如何使用NSRunningApplication
?我有与启动应用程序相反的东西:
[[NSWorkspace sharedWorkspace] launchApplication:appName];
但我想关闭一个。当我调试 NSRunningApp
的代码时,出现错误:
NSRunningApplication *selectedApp = appName;
[selectedApp terminate];
有什么问题吗?如果有请指出并如何解决。
How would I use NSRunningApplication
? I have something opposite of that which is launching an app:
[[NSWorkspace sharedWorkspace] launchApplication:appName];
but I want to close one. I get an error when I debug the code for NSRunningApp
which is this:
NSRunningApplication *selectedApp = appName;
[selectedApp terminate];
Is there something wrong? if there is please point it out and how to fix it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您为变量
selectedApp
分配一个NSString
。字符串没有- (void)terminate
方法,因此失败。您必须获取指向该应用程序的NSRunningApplication
实例。You assign the variable
selectedApp
aNSString
. Strings don't have the- (void)terminate
method and therefore it fails. You have to get aNSRunningApplication
instance pointing to the application.appName
指的是什么?如果它字面上指的是NSString
那么这是行不通的。由于 NSRunningApplication 是一个类,因此您必须创建一个实例来向其发送实例方法,就像处理任何其他类一样。
共有三个类方法(请参阅文档< /a>),您可以用来返回一个
NSRunningApplication
实例:除非您想要一个基于当前应用程序的
NSRunningApplication
实例,否则您可能会发现前两个类方法最有用。然后,您可以将
terminate
消息发送到NSRunningApplication
实例,该实例将尝试退出为其配置的应用程序。What does
appName
refer to? If it literally refers to anNSString
then that will not work.Since
NSRunningApplication
is a class, you have to create an instance to send it an instance method as you would with any other class.There are three class methods (see the docs) you can use to return an
NSRunningApplication
instance:Unless you want an
NSRunningApplication
instance based on the current application you are likely to find the first two class methods most useful.You can then send the
terminate
message to theNSRunningApplication
instance, which will attempt to quit the application that it has been configured for.