繁体   English   中英

使用ARC从NSInvocation获取块参数

[英]Get block argument from NSInvocation with ARC

我试图从NSProxy的forwardInvocation中的NSInvocation获取块参数:这是正确的语法吗? 它会泄漏内存吗?

typedef void(^SuccessBlock)(id object);
void *successBlockPointer;
[invocation getArgument:&successBlockPointer atIndex:index];
SuccessBlock successBlock = (__bridge SuccessBlock)successBlockPointer;

或者我应该使用?

typedef void(^SuccessBlock)(id object);
SuccessBlock successBlock;
[invocation getArgument:&successBlock atIndex:index];

那些像对象一样的其他参数类型呢?

__unsafe_unretained id myObject = nil; // I don't think this could be __weak? Is that correct?
[invocation getArgument:&myObject atIndex:index];

我是否必须做其他事情以正确释放分配的内存?

提前致谢。

是。 在ARC下,使用不正确

id myObject = nil; // or any object type or block type
[invocation getArgument:&myObject atIndex:index];

因为&myObject是类型id __strong * ,即指向强引用的指针。 如果指定此指针指向的强引用,则必须注意释放先前的值并保留新值。 但是, getArgument:atIndex:不会这样做。

你是对的。 你已经找到了两种正确的方法:1)用void *做它,然后将它分配回对象指针,或2)用__unsafe_unretained对象指针做。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM