簡體   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