3

What is the right way to cast Dictionary in Swift 3 to a CFDictionary? Is it legal to write like this?

 var dictionary:Dictionary<NSString, Int> = [:]

and then

 dictionary as CFDictionary
Deepak Sharma
  • 5,577
  • 7
  • 55
  • 131
  • Yes, otherwise the compiler would complain. – vadian Apr 21 '17 at 12:35
  • As i know handling `CFDictionary` directly is not suggested by apple. `NSObject`s are highly designed and optimised for `Key-Value Coding` but `CF`s not. You might miss something if you handle `CF` objects directly. – Blind Ninja Apr 21 '17 at 12:59
  • Oh thank you, I just corrected it. The question is whether it can be directly cast yo CFDictionary or not. – Deepak Sharma Apr 21 '17 at 13:14
  • 4
    @DeepakSharma Did you try it? – Hamish Apr 21 '17 at 13:16
  • 2
    @HarvantS. Some CoreFoundation functions require CF types so you have no choice. The bridge cast is perfectly fine. – vadian Apr 21 '17 at 13:22
  • I can not try, as I am porting the app to Swift, I am forced to pass a CFDictionary as input to a function part of Core Video framework. It would take another couple of thousands of line of code to be able to test whether the function behaves correctly with this typecast – Deepak Sharma Apr 21 '17 at 13:24
  • @vadian i am aware about it. but OP does not mentioned a thing about it. @DeepakSharma in that case just bridge cast you `NS` object to `CFRef`. – Blind Ninja Apr 21 '17 at 13:27
  • 2
    Of course you can try it. Simply write about 3 lines of codes and your test is done. – rmaddy Apr 21 '17 at 14:00
  • 1
    @ rmaddy trying to see if it gives correct output is no guarantee that it will always work and is as per standards. There are thousands of such examples in iOS where code works but was never part of documentation so could never be relied upon. Eventually many such code segments got broken on iOS updates leaving developers wondering. – Deepak Sharma Apr 22 '17 at 07:36
  • "of course you can try it" is poor advice because it may behave like you expect it in a test case, but still be incorrect. For example, this could cause a memory leak since CF may require an explicit release if its not managed. Your test would not show that. – wcochran May 26 '17 at 21:03

1 Answers1

10

Yes you can do that. your solution will work and you can also do it like this.

var dictionary = [

    key:value

] as CFDictionary

You can refer further from here

iOS_MIB
  • 1,885
  • 13
  • 24
  • The question remain, does one still need to call `CFRelease` on dictionary since the compiler doesn't manage CF types? – wcochran May 26 '17 at 21:14
  • 1
    You can use CFRelease but it is not recommended to use in Swift. Apple provides two methods takeUnretainedValue() and takeRetainedValue() by which you can convert unmanaged objects to managed objects. You can use this methods to convert your unmanaged that is CF objects to managed object and its memory would be managed by swift. To learn more read this : https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/WorkingWithCocoaDataTypes.html – iOS_MIB May 29 '17 at 06:25