Daily iOS
Memory and ARC

Core Foundation and ARC

When you handle a Core Foundation object returned by a C API like SecItemCopyMatching or CGImageSource in Swift, when do you use Unmanaged, takeRetainedValue(), and takeUnretainedValue()? Tie it to the Create/Get rule.

How would an interviewer hear this answer? You'll see whether they'd move on, dig in, or say no, and which sentences would trip you up. Your score doesn't change.
See the short answer

Core Foundation encodes ownership in the function name: a Create/Copy function returns a +1 value the caller owns, so you take it with takeRetainedValue(), and a Get function returns +0, so you use takeUnretainedValue(). With an Unmanaged result that choice is yours, and getting it backwards means either a leak or an over-release crash.

Share a question