17
Facilitating Idiomatic Swift with Objective-C Aaron Taylor [email protected] June 15, 2015

Facilitating Idiomatic Swift with Objective-C

Embed Size (px)

Citation preview

Page 1: Facilitating Idiomatic Swift with Objective-C

Facilitating Idiomatic Swift with Objective-C

Aaron Taylor

[email protected]

June 15, 2015

Page 2: Facilitating Idiomatic Swift with Objective-C

• Started in pure Swift

• Concision

• Type safety, esp. in data structures

• Functional aspects

Page 3: Facilitating Idiomatic Swift with Objective-C

• 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

Page 4: Facilitating Idiomatic Swift with Objective-C

getting a file’s MIMEType using UTType

vs.

Page 5: Facilitating Idiomatic Swift with Objective-C

• UnsafePointer, UnsafeMutablePointer, AutoreleasingUnsafeMutablePointer

• wrap Swift types, clutter your code

• CFunctionPointer

• unavailable in Swift*

Interacting with C APIs

Page 6: Facilitating Idiomatic Swift with Objective-C

Creating an FSEventStream

Page 7: Facilitating Idiomatic Swift with Objective-C

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

Page 8: Facilitating Idiomatic Swift with Objective-C

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

Page 9: Facilitating Idiomatic Swift with Objective-C

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

Page 10: Facilitating Idiomatic Swift with Objective-C

Bridging

• Header file used to generate Swift bindings

• @objc creates Objective-C bindings for Swift code

• as does inheriting from an Objective-C class

Page 11: Facilitating Idiomatic Swift with Objective-C

Syntactic Translation

• Intuitive conversions for initializers and methods

• unannotated initializers automatically unwrapped

• At the Cocoa level, most things just work

Page 12: Facilitating Idiomatic Swift with Objective-C

Data Types

• Type safety of Swift lost in default cocoa datatypes*

• Conversions are easy

• Some automatic, others just a cast

Page 13: Facilitating Idiomatic Swift with Objective-C

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/

Page 14: Facilitating Idiomatic Swift with Objective-C

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

Page 15: Facilitating Idiomatic Swift with Objective-C

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

Page 16: Facilitating Idiomatic Swift with Objective-C

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

Page 17: Facilitating Idiomatic Swift with Objective-C

access any file in seconds, wherever it is.

[email protected]