参考: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. 227. Basic Calculator II

    Implement a basic calculator to evaluate a simple expression string. The expression string contains ...

  2. 比较compareTo与equals及==的区别

    1.compareTo: 附上:源码: public int compareTo(String anotherString) {         int len1 = value.length;   ...

  3. ipython notebook

    pip install jupyter和pip install "ipython[all]"

  4. Fresco最最最简单使用

    http://blog.csdn.net/wa991830558/article/details/46005063

  5. JavaScript学习记录总结(五)——servlet将json数据写出去

    定义teacher和student实体 json.do   List<Student> stus=new ArrayList<Student>();        stus.a ...

  6. HDU 2072 单词数

    题目链接  http://acm.hdu.edu.cn/showproblem.php?pid=2072 普通解法: /* HDU 2072 单词数 --- 字符串处理 */ #include < ...

  7. Ubuntu 12.04 禁用触摸板

    昨天把系统换为Backbox了,版本为Ubuntu12.04,装完后发现其触摸板不能禁用,之前在其他版本都是直接快捷键就可关闭或者启用触摸板,解决方法如下: sudo add-apt-reposito ...

  8. JS/JQuery控制图片宽度

    function changeImgWidth(){ for (i = 0; i <$('#info img').length; i++) { var imgWidth=$('#info img ...

  9. Optimize Managed Code For Multi-Core Machines

    Parallel Performance Optimize Managed Code For Multi-Core Machines Daan Leijen and Judd Hall This ar ...

  10. Linux-lsof命令

    lsof,List Open Files 列出当前系统打开文件的工具.在linux环境下,任何事物都以文件的形式存在,通过文件不仅仅可以访问常规数据,还可以访问网络连接和硬件(lsof强大原因).所以 ...