psh*_*hah 13 message-forwarding nsinvocation ios automatic-ref-counting
我试图从NSProxy的forwardInvocation中的NSInvocation获取块参数:这是正确的语法吗?它会泄漏内存吗?
typedef void(^SuccessBlock)(id object);
void *successBlockPointer;
[invocation getArgument:&successBlockPointer atIndex:index];
SuccessBlock successBlock = (__bridge SuccessBlock)successBlockPointer;
Run Code Online (Sandbox Code Playgroud)
或者我应该使用?
typedef void(^SuccessBlock)(id object);
SuccessBlock successBlock;
[invocation getArgument:&successBlock atIndex:index];
Run Code Online (Sandbox Code Playgroud)
那些像对象一样的其他参数类型呢?
__unsafe_unretained id myObject = nil; // I don't think this could be __weak? Is that correct?
[invocation getArgument:&myObject atIndex:index];
Run Code Online (Sandbox Code Playgroud)
我是否必须做其他事情以正确释放分配的内存?
提前致谢.
new*_*cct 13
是.在ARC下,使用不正确
id myObject = nil; // or any object type or block type
[invocation getArgument:&myObject atIndex:index];
Run Code Online (Sandbox Code Playgroud)
因为&myObject是类型id __strong *,即指向强引用的指针.如果指定此指针指向的强引用,则必须注意释放先前的值并保留新值.但是,getArgument:atIndex:不这样做.
你是对的.你已经找到了两种正确的方法:1)用它做void *,然后将它分配回对象指针,或2)用__unsafe_unretained对象指针做.
| 归档时间: |
|
| 查看次数: |
2418 次 |
| 最近记录: |