Facilitating Idiomatic Swift with Objective-C

Post on 23-Jan-2018

1.999 views 0 download

Transcript of Facilitating Idiomatic Swift with Objective-C

Facilitating Idiomatic Swift with Objective-C

Aaron Taylor

aaron@meta.sc

June 15, 2015

• Started in pure Swift

• Concision

• Type safety, esp. in data structures

• Functional aspects

• 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

getting a file’s MIMEType using UTType

vs.

• UnsafePointer, UnsafeMutablePointer, AutoreleasingUnsafeMutablePointer

• wrap Swift types, clutter your code

• CFunctionPointer

• unavailable in Swift*

Interacting with C APIs

Creating an FSEventStream

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

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

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

Bridging

• Header file used to generate Swift bindings

• @objc creates Objective-C bindings for Swift code

• as does inheriting from an Objective-C class

Syntactic Translation

• Intuitive conversions for initializers and methods

• unannotated initializers automatically unwrapped

• At the Cocoa level, most things just work

Data Types

• Type safety of Swift lost in default cocoa datatypes*

• Conversions are easy

• Some automatic, others just a cast

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/

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”)`

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

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

access any file in seconds, wherever it is.

careers@meta.sc