toll-free-bridging

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,

ARC: __bridge versus __bridge_retained using contextInfo test case

和自甴很熟 提交于 2021-01-27 05:54:24
问题 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,

Toll-free bridging and pointer access in Swift

隐身守侯 提交于 2019-11-30 06:27:33
问题 I am porting an App from Objective-C to Swift and I need to use the following method: CFStreamCreatePairWithSocketToHost(alloc: CFAllocator!, host: CFString!, port: UInt32, \ readStream: CMutablePointer<Unmanaged<CFReadStream>?>, \ writeStream: CMutablePointer<Unmanaged<CFWriteStream>?>) The old logic looks like this (which several web sites seem to agree on): CFReadStreamRef readStream = NULL; CFWriteStreamRef writeStream = NULL; CFStreamCreatePairWithSocketToHost(NULL, (__bridge CFStringRef

Toll-free bridging and pointer access in Swift

回眸只為那壹抹淺笑 提交于 2019-11-28 18:44:15
I am porting an App from Objective-C to Swift and I need to use the following method: CFStreamCreatePairWithSocketToHost(alloc: CFAllocator!, host: CFString!, port: UInt32, \ readStream: CMutablePointer<Unmanaged<CFReadStream>?>, \ writeStream: CMutablePointer<Unmanaged<CFWriteStream>?>) The old logic looks like this (which several web sites seem to agree on): CFReadStreamRef readStream = NULL; CFWriteStreamRef writeStream = NULL; CFStreamCreatePairWithSocketToHost(NULL, (__bridge CFStringRef)(host), port, \ &readStream, &writeStream); NSInputStream inputStream = (__bridge_transfer

NSString to CFStringRef and CFStringRef to NSString in ARC?

…衆ロ難τιáo~ 提交于 2019-11-26 21:25:19
I am trying to understand the correct way of getting an NSString from a CFStringRef in ARC? Same for going the opposite direction, CFStringRef to NSString in ARC? What is the correct way to do this without creating memory leaks? Typically NSString *yourFriendlyNSString = (__bridge NSString *)yourFriendlyCFString; and CFStringRef yourFriendlyCFString = (__bridge CFStringRef)yourFriendlyNSString; Now, if you want to know why the __bridge keyword is there, you can refer to the Apple documentation . There you will find: __bridge transfers a pointer between Objective-C and Core Foundation with no

NSString to CFStringRef and CFStringRef to NSString in ARC?

梦想的初衷 提交于 2019-11-26 09:05:49
问题 I am trying to understand the correct way of getting an NSString from a CFStringRef in ARC? Same for going the opposite direction, CFStringRef to NSString in ARC? What is the correct way to do this without creating memory leaks? 回答1: Typically NSString *yourFriendlyNSString = (__bridge NSString *)yourFriendlyCFString; and CFStringRef yourFriendlyCFString = (__bridge CFStringRef)yourFriendlyNSString; Now, if you want to know why the __bridge keyword is there, you can refer to the Apple