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.
2. • Started in pure Swift
• Concision
• Type safety, esp. in data structures
• Functional aspects
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
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. 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. 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. 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. Syntactic Translation
• Intuitive conversions for initializers and methods
• unannotated initializers automatically unwrapped
• At the Cocoa level, most things just work
12. Data Types
• Type safety of Swift lost in default cocoa datatypes*
• Conversions are easy
• Some automatic, others just a cast
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. 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. 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