ARC: __bridge versus __bridge_retained using contextInfo test case

有些话、适合烂在心里 提交于 2021-01-27 05:54:27

问题


Consider this ARC code:

- (void)main {
    NSString *s = [[NSString alloc] initWithString:@"s"];
    [NSApp beginSheet:sheet 
           modalForWindow:window 
           modalDelegate:self 
           didEndSelector:@selector(sheetDidEnd:returnCode:context:) 
           contextInfo:(__bridge void *)s
    ];
}

- (void)sheetDidEnd:(NSWindow *)sheet returnCode:(NSInteger)returnCode context:(void *)context {
    NSString *s = (__bridge_transfer NSString *)context;
}

Question: on line 7, should __bridge be used, or __bridge_retained, or does it not matter, or does the choice depend on the string's retain count (that is, whether the string is explicitly alloced vs being autoreleased through a class initializer like +[NSString stringWithString:]?


回答1:


Generally, it is either

// Object to void *:
contextInfo:(__bridge void *)s

// void * to object:
NSString *s = (__bridge NSString *)context;

or

// Object to void *, retaining the object:
contextInfo:(__bridge_retained void *)s

// void * to object, transferring ownership.
// The object is released when s goes out of scope:
NSString *s = (__bridge_transfer NSString *)context;

In the first case, there is no transfer of ownership, therefore the main program must hold a strong reference to the object as long as the sheet is active.

In the second case, the object is retained when creating the sheet, and released in the sheetDidEnd: method. It is not required that the main program holds a strong reference, so this is the safe method.



来源:https://stackoverflow.com/questions/20842310/arc-bridge-versus-bridge-retained-using-contextinfo-test-case

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!