标签: nsinvocation

NSInvocationOperation使用params定义选择器

我正在尝试创建NSInvocationOperation,以便它应该使用params调用object的方法

- (void) getImages: (NSRange) bounds
{
    NSOperationQueue *queue = [NSOperationQueue new];
    NSArray * params = [NSArray arrayWithObjects:
          [[NSNumber alloc] initWithInt: bounds.location],
          [[NSNumber alloc] initWithInt: bounds.length]];
    NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self
                   selector:@selector(loadImagesWithOperation)
                     object:params];

    [queue addOperation:operation];
    [operation release];

}

- (void) loadImagesWithOperation:(NSArray*)bounds {
 NSLog(@"loadImagesWithOperation");
}
Run Code Online (Sandbox Code Playgroud)

此代码与EXC_BAD_ACCESS崩溃.如果我改变要调用的函数的定义

- (void) loadImagesWithOperation {
 NSLog(@"loadImagesWithOperation");
}
Run Code Online (Sandbox Code Playgroud)

一切都变好了.我试图在@selector的代码块中使用不同的语法,如@selector(loadImagesWithOperation :)@selector(loadImagesWithOperation:bounds :),但没有成功.

使用params定义选择器和函数的正确方法是什么?

谢谢.

objective-c selector nsinvocation function-declaration ios

2
推荐指数
1
解决办法
5308
查看次数

NSInvocation setArgument不能使用简单的int32_t

我在使用NSInvocation时使用非对象的参数时遇到问题.我传递的简单整数值变为不同的东西.

这是我调用的方法:

+(NSString*) TestMethod2 :(int32_t) number
{
    NSLog(@"*************** %d ******************", number);
    return @"success";
}
Run Code Online (Sandbox Code Playgroud)

这就是我所说的:

-(void) TestInvocationUsingReflection
{
    id testClass = NSClassFromString(@"TestClass");
    NSMethodSignature * methodSignature = [testClass methodSignatureForSelector:@selector(TestMethod2:)];

    NSInvocation *inv = [NSInvocation invocationWithMethodSignature:methodSignature];
    [inv setSelector:@selector(TestMethod2:)];
    [inv setTarget:testClass];
    NSNumber * arg = [NSNumber numberWithInt:123456];

    [inv setArgument:&arg atIndex:2]; //arguments 0 and 1 are self and _cmd respectively, automatically set by NSInvocation
    [inv retainArguments];
    NSUInteger length = [[inv methodSignature] methodReturnLength];
    id result = (void *)malloc(length);

    [inv invoke];
    [inv getReturnValue:&result];

}
Run Code Online (Sandbox Code Playgroud)

结果是记录的不是我传递的简单123456值,但是这样的事情:

*** …

integer objective-c nsinvocation

2
推荐指数
1
解决办法
3048
查看次数

Swallow 无法识别来自 NSInvocation forwardInvocation 调用的选择器异常

我有一个正在转发接收消息的对象。它没有实现使用forwardInvocation. 但是,methodSignatureForSelector由于程序的组织方式,在某些时候不会总是返回有效的方法签名。如何吞下由缺少的方法签名生成的异常?覆盖doesNotRecognizeSelector不起作用。谢谢。

objective-c objective-c-runtime message-forwarding nsinvocation

2
推荐指数
1
解决办法
548
查看次数

ARC [rewriter] NSInvocation的setArgument与不具有__unsafe_unretained的所有权的对象一起使用是不安全的

我一直把我的项目转换成ARC,我坚持这个错误.

与对象,与调用和&callerToRetain正显示出我的错误"[重写] NSInvocation的的setArgument是不是安全,可以使用一个对象,具有比其他__unsafe_unretained所有权"

+ (void)performSelector:(SEL)selector onTarget:(id *)target withObject:(id)object amount:(void *)amount callerToRetain:(id)callerToRetain{if ([*target respondsToSelector:selector]) {
    NSMethodSignature *signature = nil;
    signature = [*target methodSignatureForSelector:selector];
    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];

    [invocation setSelector:selector];

    int argumentNumber = 2;

    // If we got an object parameter, we pass a pointer to the object pointer
    if (object) {
        [invocation setArgument:&object atIndex:argumentNumber];
        argumentNumber++;
    }

    // For the amount we'll just pass the pointer directly so NSInvocation will call the method using the number itself rather than a pointer …
Run Code Online (Sandbox Code Playgroud)

nsinvocation automatic-ref-counting

1
推荐指数
1
解决办法
1736
查看次数