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

2,044 views

Published on

Blog Post: https://www.meta.sc/tech/swiftobjc

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 aaron@meta.sc 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 https://developer.apple.com/library/ios/documentation/Swift/Conceptual/ 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 • blog.cocoapods.org/CocoaPods-0.36/ • blog.cocoapods.org/Pod-Authors-Guide-to-CocoaPods-Frameworks/
  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 • https://developer.apple.com/library/ios/documentation/Swift/Conceptual/ BuildingCocoaApps/WorkingWithCocoaDataTypes.html • https://developer.apple.com/library/ios/documentation/Swift/Conceptual/ BuildingCocoaApps/InteractingWithCAPIs.html • https://developer.apple.com/library/ios/documentation/Swift/Conceptual/ BuildingCocoaApps/MixandMatch.html • https://developer.apple.com/swift/blog/?id=6 • http://dev.eltima.com/post/93497713759/interacting-with-c-pointers-in-swift- part-2 • http://www.charlessoft.com/hacks/using_c_function_pointers_in_swift.html
  17. 17. access any file in seconds, wherever it is. careers@meta.sc

×