Swift provides improved type safety, code safety, and interoperability with Objective-C. It allows bridging between the two languages for migration and taking advantage of existing Objective-C APIs and Cocoa patterns. eHarmony plans to tackle migrating to Swift by modernizing their Objective-C code, generating bridging headers, and incrementally migrating and replacing Objective-C files with equivalent Swift code and classes.
Swift LA Meetup at eHarmony- Swift and Enterprise and eHarmony with Heena Rastogi
1. Swift & Enterprise & eHarmony
• Why?
• Bridging & Migration
• eHarmony’s Plan to Tackle
2. • Simpler to read
• Type safety and hence code safety
• Interacts with Objective-C Runtime seamlessly
• Take advantage of Objective-C APIs and Cocoa design patterns
• Open Source - we gain flexibility and freedom
• Other platforms can also adopt Swift
Why?
3. 3 important aspects of Swift / Objective-C compatibility:
• Interoperability
Ability to interface between Swift and Objective-C in either direction
• Mix and Match
Allows you to create mixed-language apps containing both Swift and
Objective-C files that can communicate with each other
• Migration
Migration from existing Objective-C code to Swift is made easy with
interoperability and mix and match, making it possible to replace parts
of your Objective-C apps with the latest Swift features
Bridging & Migration
4. 3 New features introduced for Objective-C :
• Nullability for Objective-C
• Lightweight Generics
• Kind of
Bridging & Migration
5. Bridging & Migration
Objective-C
@interface SNGFeedView
@property(nonatomic, readonly) UIView *photoView;
@property(nonatomic, readonly, copy) NSArray *feedViews;
- (UIView*) photoViewForFeed:(SSFeed) photoFeed;
@end
Swift
class SNGFeedView {
var photoView : UIView?
var feedViews: [AnyObject]!
func photoView(photoFeed:SSFeed) - > UIView
}
6. Indicate whether Objective-C/C pointers can be nil
• Better communicate intent of APIs
• Allows improved static checking
• Improves usability of APIs in Swift
Nullability
7. Nullability Qualifiers
Nullability
Qualifier Usage Swift
nullable Pointer may be nil UIView?
nonnull nil is not a meaningful value UIView
null_unspecified Neither nullable nor nonnull
applies
UIView!
NOTE: Compiler does not change the way it generates
code because of a non-null annotation.
8. Example:
NS_ASSUME_NONNULL_BEGIN
@interface SNGFeedView
@property(nonatomic,readonly,nullable) UIView *photoView;
@property(nonatomic, readonly,copy) NSArray *feedViews;
-(UIView*) photoViewForFeed:(SSFeed) photoFeed;
@end
NS_ASSUME_NONNULL_END
Audited regions make default assumptions about some
pointers:
• Single-level pointers are assumed to be nonnull
• NSError** parameters are assumed to be nullable for both
levels
• Only annotate the nullable or null_unspecified cases
Nullability
9. Allow collections to be parameterized by element type:
“An array of views” ,
“A dictionary mapping strings to images”
• Improve expressivity of APIs
• Make collections easier to use
• Enable better static type checking
Lightweight Generics
11. • It tells compiler it’s object of some kind of of given type
extern __kindof NSApplication *NSApp; // NSApplication instance
NSObject *object = NSApp; // convert to super class
MyApplication *myApp = NSApp; // implicit downcast subclass
NSString *string = NSApp // Incorrect
• Much more useful than id, more type information in API
contract
• Allows messaging subclass methods
[NSApp praiseUser] // Invokes MyApplication method
KindOf Types
12. • Modernization of Objective-C
• Generating Bridging Headers
• Incremental Migration
eHarmony’s Plan to Tackle
13. Before you start:
• Create a Swift class for your corresponding Objective-C .m and .h
• Import relevant system frameworks
• Fill out an Objective-C bridging header if you need to access
Objective-C code from the same app target in your Swift file
• To make your Swift class accessible and usable back in Objective-
C, make it a descendant of an Objective-C class or mark it with the
@objc attribute
Migration
14. As you work:
• Set up your Swift class by subclassing Objective-C classes,
adopting Objective-C protocols, and more.
• See “Adopting Cocoa Design Patterns” for information on
translating common design patterns.
• To translate your properties from Objective-C to Swift, read
Properties in “The Swift Programming Language.”
• Declare simple macros as global constants, and translate
complex macros into functions.
Migration
15. After you finish:
• Update import statements in your Objective-C code (to #import
"ProductModuleName-Swift.h"), as described in “Importing Code
from Within the Same App Target”
• Remove the original Objective-C .m file from the target by
deselecting the target membership checkbox
• Update your code to use the Swift class name instead of the
Objective-C name if you gave the Swift class a different name
Migration