【问题标题】:pass multiple arguments in sector with NSthread in objective c [duplicate]在目标c中使用NSthread在扇区中传递多个参数[重复]
【发布时间】:2013-02-07 07:34:01
【问题描述】:

我想通过 NSThread 将两个参数传递给方法。我尝试使用this third answer。但不幸的是,代码运行不佳,因为andKeys: 方法不可用。

   [NSThread detachNewThreadSelector:@selector(DownloadCheckServerPath:DirectoryName:) toTarget:self withObject:"How to pass two objects"];

那么我应该如何通过NSThread在Sector中调用-(void)DownloadCheckServerPath:(NSString *)serverPath DirectoryName:(NSString *)directoryName这个方法。 ?

【问题讨论】:

标签: ios objective-c nsthread


【解决方案1】:

您不能将多个参数传递给此消息发送调用的方法。解决方法:使用集合。一个论据就足够了。

- (void)reallyCallMethod:(NSDictionary *)dict
{
    [self downloadCheckServerPath:dict[@"path"] directoryName:dict[@"dir"]];
}

[NSThread detachNewThreadSelector:@selector(reallyCallMethod:)
                         toTarget:self
                       withObject:@{ @"path" : @"/foo/bar", @"dir" : @"baz" }];

另外:不要以大写字母开头的选择器名称。这仅适用于类(而且它真的很丑,不是吗?)

【讨论】:

  • 是的!现在功能工作正常。谢谢你的回答。我仍然是目标 c 的初学者。就是这样。 :)
【解决方案2】:

在这种情况下,我通常会创建一个 NSDictionary 并通过它。在函数中,我阅读了字典并获取了我需要的所有元素。

【讨论】:

    猜你喜欢
    • 2012-07-25
    • 2023-03-28
    • 2014-11-07
    • 2017-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-08
    • 1970-01-01
    相关资源
    最近更新 更多