简体   繁体   中英

Block + ARC, stack and heap

I want to associate a block with some identifier, and store it in a dictionary for later retrieval.

Am I doing this wrong? Basically I'm worried that I might be passing in a block that is declared on the stack via setAction:forProduct, and the reference will later be invalidated. Is ARC smart enough to catch this?

typedef void (^ProductPurchased)();

-(void) setAction:(ProductPurchased)action forProduct:(NSString*)identifier;

@property (strong, nonatomic) NSMutableDictionary *actions;

-(void) setAction:(ProductPurchased) action forProduct:(NSString*)identifier
{
    [self.actions setObject:action forKey:identifier];
}

See the answer to this . You need to use Block_copy() before adding it to the dictionary and Block_release() after removing it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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