参考:http://blog.csdn.net/dqjyong/article/details/7677557

参考:http://stackoverflow.com/questions/2327617/calling-selector-with-two-arguments-on-nsthread-issue

使用NSInvocation对象

NSInvocation* deleteInvocation = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:@selector(deleteDataAtPath:)]];
[deleteInvocation setTarget:self];
[deleteInvocation setSelector:@selector(deleteDataAtPath:)];//给NSInvocation对象添加对应的动作
// // self, _cmd, ... 参数索引必须是2以后
[deleteInvocation setArgument:&cachePath atIndex:];
//用NSInvocation对象来初始化一个NSOperation的子类NSInvocationOperation对象
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithInvocation:invoction]; //初始化一个操作队列
NSOperationQueue* operationQueue=[[NSOperationQueue alloc] init]; //在操作队列中加入操作
[operationQueue addOperation:operation];
[operation release];

具体实现:

NSThread+ManyObjects.h:

@interface NSThread (ManyObjects)

+ (void)detachNewThreadSelector:(SEL)aSelector
toTarget:(id)aTarget
withObject:(id)anArgument
andObject:(id)anotherArgument; @end

NSThread+ManyObjects.m:

@implementation NSThread (ManyObjects)

+ (void)detachNewThreadSelector:(SEL)aSelector
toTarget:(id)aTarget
withObject:(id)anArgument
andObject:(id)anotherArgument
{
NSMethodSignature *signature = [aTarget methodSignatureForSelector:aSelector];
if (!signature) return; NSInvocation* invocation = [NSInvocation invocationWithMethodSignature:signature];
[invocation setTarget:aTarget];
[invocation setSelector:aSelector];
[invocation setArgument:&anArgument atIndex:];
[invocation setArgument:&anotherArgument atIndex:];
[invocation retainArguments]; [self detachNewThreadSelector:@selector(invoke) toTarget:invocation withObject:nil];
} @end

调用:

[NSThread detachNewThreadSelector:@selector(loginWithUser:user:password:) toTarget:self withObject:@"someusername" andObject:@"somepassword"];

另一种实现:

参考:http://blog.engledew.com/post/730804661/nsthread-a-selector-with-multiple-arguments

#import <Foundation/Foundation.h>

@interface SEThreadInvocation : NSObject
{
NSThread * _thread;
NSInvocation * _invocation;
} @property(nonatomic,retain) NSThread * thread;
@property(nonatomic,retain) NSInvocation * invocation; - (id) initWithTarget:(id)target selector:(SEL)selector arguments:(NSArray *)arguments;
+ (void) detachNewThreadSelector:(SEL)selector toTarget:(id)target withObjects:(id)firstObject, ... NS_REQUIRES_NIL_TERMINATION;
- (void) start; @end
#import "SEThreadInvocation.h"

@implementation SEThreadInvocation

@synthesize thread = _thread;
@synthesize invocation = _invocation; - (id) initWithTarget:(id)target selector:(SEL)selector arguments:(NSArray *)arguments
{
if (self = [super init])
{
_thread = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];
_thread.name = @"SEThreadInvocation"; NSMethodSignature * signature = [target methodSignatureForSelector:selector]; self.invocation = [NSInvocation invocationWithMethodSignature:signature]; [_invocation setTarget:target];
[_invocation setSelector:selector]; NSInteger i = ; // self, _cmd, ... for (id argument in arguments)
{
[_invocation setArgument:&argument atIndex:i++];
} [_invocation retainArguments];
}
return self;
} - (void)start
{
[_thread start];
} - (void)run
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; [_invocation invoke]; [pool release];
} + (void) detachNewThreadSelector:(SEL)selector toTarget:(id)target withObjects:(id)firstObject, ...
{
NSMutableArray * arguments = [NSMutableArray array]; if (firstObject)
{
[arguments addObject:firstObject]; id argument;
va_list objectList;
va_start(objectList, firstObject); while (argument = va_arg(objectList, id))
{
[arguments addObject:argument];
} va_end(objectList);
} SEThreadInvocation * threadInvocation = [[SEThreadInvocation alloc] initWithTarget:target selector:selector arguments:arguments];
[threadInvocation start];
[threadInvocation autorelease];
} - (void) dealloc
{
[_invocation release];
[_thread release]; [super dealloc];
} @end
[SEThreadInvocation detachNewThreadSelector:@selector(request:processData:)
toTarget:self
withObjects:request, data, nil];

使用NSOperation使用,创建线程中传递多个参数的更多相关文章

  1. c语言线程中传输多个参数

    前言:c语言中创建一条线程,但是需要传送多个参数给线程的话我们自然会想到通过传送数组或者结构体来实现,下面我们来看看如何在创建线程的时候传送结构体和数组. #include <stdio.h&g ...

  2. MyBatis 中传递多个参数的 4 种方式

    方式 1 :封装成对象入参  #{对应实体类的属性} //UserMapper.java 接口 /** * 多条件查询:根据用户名称(模糊查询)和用户角色查询用户列表(参数:对象入参) * @para ...

  3. Mybatis中传递多个参数的方法总结

    一.单个参数: public List<XXBean> getXXBeanList(String xxCode); <select id="getXXXBeanList&q ...

  4. Mybatis接口中传递多个参数

    1.接口 public interface MemberMapper { public boolean insertMember(Members member); public Members sel ...

  5. C# 中传递多个参数给多线程

    1.方式一:使用ParameterizedThreadStart委托 如果使用了ParameterizedThreadStart委托,线程的入口必须有一个object类型的参数,且返回类型为void. ...

  6. struts2 action中传递两个参数到url

    <action name="outInDetail" class="formManage_outInDetailAction"> <resul ...

  7. vue v-show与v-for同时配合v-bind使用并在href中传递多个参数的使用方法

    最近在项目中,因为还没使用前端构建工具,还在使用vue+jquery方法渲染页面 碰到几个小问题,在此记录下作为vue学习之路上的一个小知识点 需求:1.数据列表存在与否状态,没有数据显示默认提示,有 ...

  8. A标签中传递的中文参数到Servlet 后台request.getParameter()接收时出现中文乱码

    package util; import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletRequ ...

  9. CreateThread与_beginthread, _beginthreadex创建线程的基本概念和区别(1)

    这三个函数都可以创建新的线程,但都是如何创建的呢?当然MSDN文档最权威: Creates a thread to execute within the virtual address space o ...

随机推荐

  1. SQL注入测试平台 SQLol -6.CHALLENGES挑战

    SQLol上面的挑战共有14关,接下来我们一关一关来突破. Challenge 0 目的是让查询返回所有的用户名,而不是只有一个. SELECT username FROM users WHERE u ...

  2. Chrome A标签的迁移错误:【Error loading page】

    在IE中经常使用A标签用来迁移,正确的写法是 <a href="001.html"></a>即可,不过在chrome上面可能会引发错误无法迁移. 比如用下面 ...

  3. JQuery之滑动幻灯片插件Easy Slider初体验

    Easy Slider 是一个滑动幻灯片插件,支持任何图片或内容,可以实现横向或纵向滑动.它拥有一系列丰富的参数设置,可通过CSS来进行完全的控制.基本上只需要引入这个插件后,设置好内容,然后样式化C ...

  4. php特殊语法--模板引擎中比较常见

    <?php $a=array(1,2,0); foreach ($a as $v): if($v>1): ?> 5 <?php endif; endforeach; ?> ...

  5. CoordinatorLayout-带图片伸缩工具栏

    伸缩工具栏toolbardesign 效果图: 步骤一: 在build.gilde中添加以下代码 dependencies { compile fileTree(dir: 'libs', includ ...

  6. 001. 启动Visual Studio 2010报错

    错误内容: 外接程序"VMDebugger"未能加载或导致了异常. 是否希望移除该外接程序? 错误号: 80004005 如下图: 解决办法一: 1. 点击 是 打开VS2010, ...

  7. python3多线程趣味详解

    python3的多线程很多人无法理解是怎么运行的,因此本文从程序猿的日常生活出发,写了一个由浅入深的多线程教程,这样子大家就不会觉得陌生了,多线程真的很简单很简单! 不要讲多线程局限于库或者框架,自己 ...

  8. IIS不定期Crash和Oracle“未处理的内部错误(-2)”的问题分析

    问题描述:系统不定期报出Oracle“未处理的内部错误(-2)”,严重时IIS会Crash 典型异常日志如下: Exception type:   System.AccessViolationExce ...

  9. Protocol Buffer基本介绍

    转自:http://www.cnblogs.com/stephen-liu74/archive/2013/01/02/2841485.html 该系列Blog的内容主体主要源自于Protocol Bu ...

  10. DW(五):polybase集群安装

    目录: Prerequisites 集群配置规划 polybase install firewall config 集群配置 删除计算节点 install Prerequisites Microsof ...