Last active
March 30, 2018 18:23
-
-
Save mrh-is/e6ac28c40b52259ce8ee8275f4d525ad to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
protocol ObjCConvertible {} | |
extension String: ObjCConvertible {} | |
extension Int: ObjCConvertible {} | |
extension Double: ObjCConvertible {} | |
extension Bool: ObjCConvertible {} | |
func objCMethod(_ dict: [String: AnyObject]) { | |
print(dict) | |
} | |
func passToObjC(_ dict: [String: ObjCConvertible]) { | |
objCMethod(dict as [String: AnyObject]) | |
} | |
passToObjC(["a": "b", "c": 1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sugar for building
[String: AnyObject]
dictionaries from Swift, mostly for passing to ObjC.This works great for things that are actually
_ObjectiveCBridgeable
! Compiler magic does the boxing the right way. But anything else (custom structs/enums, other non-ObjC stuff) will get passed along also, and blow up when you try to touch them from ObjC.Be careful with this, and only ever let things you know to be safely representable in ObjC conform to
ObjCConvertible
.