NSTask 只执行一次
我在执行不同的 NSTask 时遇到问题。相同的launchPath
,不同的参数
。我有一个类,其实例管理自己的 NSTask 对象,并根据参数初始化这些实例 - 正在创建依赖的 NSTask 对象。我有两个初始值设定项:
// Method for finished task
- (void)taskFinished:(NSNotification *)aNotification {
[myTask release];
myTask = nil;
[self createTask];
}
// Designated initializer
- (id) init {
self = [super init];
if (self != nil) {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(taskFinished:)
name:NSTaskDidTerminateNotification
object:nil];
[self createTask];
}
return self;
}
// Convenience initializer
- (id)initWithCommand:(NSString *)subCommand {
self = [self init];
if (self)
{
[self setCommand:subCommand];
}
return self;
}
这是 createTask
方法:
- (void)createTask {
// myTask is a property defined as NSTask*
myTask = [[NSTask alloc] init];
[myTask setLaunchPath:@"/usr/bin/executable"];
}
通过在 NSOutlineView 中选择不同的行来执行操作(使用 PXSourceList 作为包装器):
- (void)sourceListSelectionDidChange:(NSNotification *)notification {
id sourceList = [notification object];
NSIndexSet *selection = [sourceList selectedRowIndexes];
NSString *identifier = [[sourceList itemAtRow:[selection firstIndex]] identifier];
// this way `/usr/bin/executable ${identifier}` is being created
MyCommand *command = [[MyCommand alloc] initWithSubcommand:identifier];
// this method executes [myTask launch];
[command execute]
}
问题是只有第一个被执行。第二个甚至不会触发“点击”事件(通过目标操作)。我认为这可能是我尝试使用 launchPath 的原因,因为简单的 /bin/ls
工作正常。终端中的相同命令有 0 返回值(即一切正常)。非常感谢任何指南或陷阱。
I'm having trouble executing different NSTask's. Same launchPath
, different arguments
. I have a class who's instances administer own NSTask
objects and depending on arguments those instances were initialized with - dependent NSTask
object is being created. I have two initializers:
// Method for finished task
- (void)taskFinished:(NSNotification *)aNotification {
[myTask release];
myTask = nil;
[self createTask];
}
// Designated initializer
- (id) init {
self = [super init];
if (self != nil) {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(taskFinished:)
name:NSTaskDidTerminateNotification
object:nil];
[self createTask];
}
return self;
}
// Convenience initializer
- (id)initWithCommand:(NSString *)subCommand {
self = [self init];
if (self)
{
[self setCommand:subCommand];
}
return self;
}
And here 's the createTask
method:
- (void)createTask {
// myTask is a property defined as NSTask*
myTask = [[NSTask alloc] init];
[myTask setLaunchPath:@"/usr/bin/executable"];
}
The actions are executed via selecting different rows in NSOutlineView (using PXSourceList as a wrapper):
- (void)sourceListSelectionDidChange:(NSNotification *)notification {
id sourceList = [notification object];
NSIndexSet *selection = [sourceList selectedRowIndexes];
NSString *identifier = [[sourceList itemAtRow:[selection firstIndex]] identifier];
// this way `/usr/bin/executable ${identifier}` is being created
MyCommand *command = [[MyCommand alloc] initWithSubcommand:identifier];
// this method executes [myTask launch];
[command execute]
}
The problem is that only first one gets executed. The second ones does not even trigger "click" event (via target-action). I think it could be cause of launchPath I'm trying to use, 'cause simple /bin/ls
works fine. The same command in terminal has 0 return value (i.e. all is fine). Any guides or gotchas are much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我无法理解为什么...但是已经阅读了许多地方< /a> NSTask 只能运行一次......
这看起来很荒谬...如果我发现任何与此来源相矛盾的信息,我会研究并发回,(尽管它通常是可靠的。)
I can't comprehend why... but have read from numerous places that NSTask CAN ONLY be run once....
This seems ridiculous... I will research and post back if I find any info the contradicts this source, (although it is usually reliable.)
如果您愿意涉足 PyObjC 周围的浑水,您可以轻松使用 Python 的
子进程
机制......一遍又一遍。哦,是的。If you care to wade the murky waters that surround PyObjC, you can easily use the
subprocess
mechanism of Python.. over, and over, and over. Oh yeah.