Questions tagged [toll-free-bridging]

20 questions
90
votes
1 answer

NSString to CFStringRef and CFStringRef to NSString in ARC?

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?
zumzum
  • 13,218
  • 19
  • 89
  • 125
23
votes
5 answers

Toll-free bridging and pointer access in Swift

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?>, \ writeStream:…
Ephemera
  • 7,828
  • 7
  • 38
  • 79
8
votes
1 answer

Bridging to/from CoreFoundation on Linux

I'm trying to compile some code on Linux that uses both CoreFoundation and Foundation, but Linux doesn't implement bridging in the same way macOS and iOS do. Bridging between Objective-C and Swift works: import Foundation import…
NobodyNada
  • 7,002
  • 6
  • 39
  • 48
5
votes
1 answer

CFDictionary won't bridge to NSDictionary (Swift 2.0 / iOS9)

OK, this is a case I came across when working with CGImageSource and noticed that the toll-free-bridging between CFDictionary and NSDictionary seems to run into problems in certain cases. I've managed to construct the below example to show what I…
5
votes
1 answer

ARC: __bridge versus __bridge_retained using contextInfo test case

Consider this ARC code: - (void)main { NSString *s = [[NSString alloc] initWithString:@"s"]; [NSApp beginSheet:sheet modalForWindow:window modalDelegate:self …
4
votes
1 answer

Are Swift's String, Array, and Dictionary NSObjects? Why can a struct be an NSObject?

I use NSKeyedArchiver.archivedDataWithRootObject(obj) to transform an object to NSData. The archivedDataWithRootObject(obj) method require its parameter to be an NSObject, conforming to NSCoding. I tried archiving Swift Strings, Arrays, and…
huync
  • 7,009
  • 4
  • 31
  • 42
4
votes
4 answers

Is a __bridge_transfer valid on a NULL object

Let's say a method returns a CFErrorRef via a pointer. This returned error may be NULL. So would it be safe to perform a __bridge_transfer still or should I check for NULL. E.g. CFErrorRef cfError; ABAddressBookRef addressBookRef =…
Awesome-o
  • 1,842
  • 1
  • 23
  • 35
3
votes
1 answer

Correct way to handle CGImageRef in containers under ARC

I am new to incorporating arc in my projects. I am trying to understand __bridge and its little friends so that I can properly cast my CGImageRef's when adding and removing them from containers. I am getting a "Potential leak of an object stored…"…
3
votes
1 answer

Safe to use matching CFBridging calls to transfer in and out of ARC for non CF types?

I'm writing a game using the Chipmunk physics engine, and I'd like to store a pointer to an Objective-C object in every body's userData field. I know I need to use a bridging cast to cast between id and void *, but I'm not sure the way I'm doing it…
godel9
  • 7,179
  • 1
  • 30
  • 51
3
votes
1 answer

OS X: How to correctly bridge release pointer with __bridge_transfer

I have a block object that is converted to a void * pointer to pass to a C library. I'm deploying to OS X 10.6 so CFBridgingRetain and CFBridgingRelease are not available. Instead I'm doing: void ptr_retain(void (^block)()) { void *ptr =…
3
votes
1 answer

Converting CFIndex to NSInteger

This question is similar to mine, but does not contain an answer. I want to convert a CFIndex into an NSInteger. I have checked the list of types that have toll-free bridging and this is not one of them.
2
votes
1 answer

ObjC casting a block with toll-free-bridged types

Is it possible to pass a block that takes Objective-C types which have Toll-free-bridged equivalents directly into a method expecting a block with the C-types? Concrete example I'm wrapping the AddressBook and Contacts APIs depending on…
Stephen Groom
  • 3,449
  • 1
  • 10
  • 23
2
votes
2 answers

NSFastEnumeration object casting in ARC

I'm trying to implement the countByEnumeratingWithState:objects:count: method from the NSFastEnumeration protocol on a custom class. So far I have it iterating through my objects correctly, but the objects that are returned aren't Objective-C…
1
vote
0 answers

(__bridge__transfer NSData*) returns _NSCFArray instead of NSData

I have some code I am using for keychain management. The SecItemCopyMatching method returns the result in (CFTypeRef *) type called foundDict, which is passed as a reference to the SecItemCopyMatching method. Then I am using (__bridge_transfer…
malena
  • 690
  • 8
  • 24
1
vote
1 answer

Assigning from a Class method binds the class instead of its return value

I've noticed something strange in the behavior of code I'm currently writing and thought I would ask here to see if I'm doing something silly that would cause this to happen. Basically, when I assign a variable to the return value of my class…
1
2