-1

I have this code:

NSWorkspace *sharedWorkspace = [NSWorkspace sharedWorkspace];
NSString *appPath = [sharedWorkspace fullPathForApplication:appName];
NSString *identifier = [[NSBundle bundleWithPath:appPath] bundleIdentifier];
NSArray *selectedApps =
    [NSRunningApplication runningApplicationsWithBundleIdentifier:identifier];
// quit all
[selectedApps makeObjectsPerformSelector:@selector(terminate)];

which is suppose to close any application running from just the name which is: appName (NSString).

When I debug the app and type in the application name into the NSTextField pointing towards appName, it closes my application instead of the other application I want it to terminate. I replied this question on another post but no one is responding so I thought maybe I can get a response if I start a new post... thanks. (THIS IS NOT A DUPLICATE, its just that people don't respond when I reply bakc...)

Thanks.,

Kevin

4

1 回答 1

1

在项目的 Info.plist 中检查应用程序的 Bundle Identifier 并确保它是唯一的。

此外,您应该确定您实际作为参数传递的 selectedApps 的值。为此,请记录它:

NSLog(@"selectedApps: %@", selectedApps); 

或者(这是 Jon Hess 在评论中建议的),创建一个断点。您可以通过以下几种方式做到这一点:

设置断点后,选择 Run->Debug。这将在调试器 (gdb) 中执行您的程序。做任何你通常做的事情来达到失败的程度。但是,这一次它不会终止任何东西,而是会停在您指定的行。此时您可以检查您的变量。您可以通过发出“po”(打印对象)命令来打印objective-c 实例。因此,您最终可能会得到以下结果:

(gdb)po appPath
    // gdb will print this
(gdb)po identifier
    // gdb will print this
(gdb)po selectedApps
    // gdb will print this
于 2009-09-29T00:17:42.187 回答