Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Facilitating Idiomatic Swift with Objective-C


Published on

Blog Post:

If you are hitting roadblocks in trying to communicate with Core Foundation and C APIs directly from Swift, just wrap them in a friendly Objective-C class that provides an simple API for the rest of your application to use.

Published in: Technology
  • Be the first to comment

Facilitating Idiomatic Swift with Objective-C

  1. 1. Facilitating Idiomatic Swift with Objective-C Aaron Taylor June 15, 2015
  2. 2. • Started in pure Swift • Concision • Type safety, esp. in data structures • Functional aspects
  3. 3. • Unmanaged data types for unannotated APIs • All of the ones we are working with • Documentation issues • Fix the compiler errors, then get runtime errors • Lots of head scratching Core Foundation in Swift
  4. 4. getting a file’s MIMEType using UTType vs.
  5. 5. • UnsafePointer, UnsafeMutablePointer, AutoreleasingUnsafeMutablePointer • wrap Swift types, clutter your code • CFunctionPointer • unavailable in Swift* Interacting with C APIs
  6. 6. Creating an FSEventStream
  7. 7. Benefits of Swift are lost • No longer writing idiomatic Swift • Type safety (mostly) gone • Objective-C is more concise • Basically writing C with more verbose syntax
  8. 8. Reclaim the benefits of Swift • Wrap Core Foundation and C interactions with Objective-C classes providing specific functionality • For us: FileSystemListener and FileSystemCrawler • Provide a clean API for your project’s use
  9. 9. Objective-C and Swift • Bridging by the compiler • Syntactic Translation • Data types • Cocoapods BuildingCocoaApps/index.html
  10. 10. Bridging • Header file used to generate Swift bindings • @objc creates Objective-C bindings for Swift code • as does inheriting from an Objective-C class
  11. 11. Syntactic Translation • Intuitive conversions for initializers and methods • unannotated initializers automatically unwrapped • At the Cocoa level, most things just work
  12. 12. Data Types • Type safety of Swift lost in default cocoa datatypes* • Conversions are easy • Some automatic, others just a cast
  13. 13. Cocoapods • Pods written in Swift are treated as dynamically linked frameworks • Requires use_frameworks! in your Podfile • •
  14. 14. Remaining Issues • Some type safety lost in interop with swift code • New Objective-C generics help! • Compiler optimization turned off • significant performance hit, orders of magnitude difference in some cases • Look into `@_semantics(“optimize.sil.never”)`
  15. 15. Xcode 7 • Support for CFunctionPointer from closures and swift functions • Native support for C function pointers: C functions that take function pointer arguments can be called using closures or global functions, with the restriction that the closure must not capture any of its local context. (16339559) • Generics in Objective-C • Typed data structures translate to swift
  16. 16. References • BuildingCocoaApps/WorkingWithCocoaDataTypes.html • BuildingCocoaApps/InteractingWithCAPIs.html • BuildingCocoaApps/MixandMatch.html • • part-2 •
  17. 17. access any file in seconds, wherever it is.