Swift meetup22june2015
-
Upload
claire-townend-gee -
Category
Software
-
view
55 -
download
0
Transcript of Swift meetup22june2015
![Page 1: Swift meetup22june2015](https://reader030.fdocuments.net/reader030/viewer/2022032700/55d27f79bb61eb7f078b461f/html5/thumbnails/1.jpg)
Swift LA22 June 2015
eHarmony
![Page 2: Swift meetup22june2015](https://reader030.fdocuments.net/reader030/viewer/2022032700/55d27f79bb61eb7f078b461f/html5/thumbnails/2.jpg)
• Introduction• What You Might Have Missed at WWDC• Swift 2.0• Swift & Enterprise & eHarmony• Questions?
Agenda
![Page 3: Swift meetup22june2015](https://reader030.fdocuments.net/reader030/viewer/2022032700/55d27f79bb61eb7f078b461f/html5/thumbnails/3.jpg)
“We came together in 2000 believing that with a mix of psychology and some cutting edge technology, we could create relationships that were happier, more fulfilling and enduring.With over 61 million registered users, we’ve successfully married more than 565,000 couples, and are responsible for nearly 5% of marriages in the US — that’s 438 people every day that say ‘I Do’ because of eHarmony.”
“Millions of people have used eHarmony’s Compatibility Matching System to find compatible long-term relationships. Today, an average of 438 eHarmony members marry every day in the United States as a result of being matched on the site.”
Who is eHarmony?
![Page 4: Swift meetup22june2015](https://reader030.fdocuments.net/reader030/viewer/2022032700/55d27f79bb61eb7f078b461f/html5/thumbnails/4.jpg)
Currently, we maintain Desktop Web, Mobile Web, iOS and Android apps for our scientific matching service.
Our iOS App currently boasts:• 8 billion downloads• Best selling app• Biggest grossing app
The team has been busy!
The eHarmony App
![Page 5: Swift meetup22june2015](https://reader030.fdocuments.net/reader030/viewer/2022032700/55d27f79bb61eb7f078b461f/html5/thumbnails/5.jpg)
• Dr. Gary Philipp – Sr. Manager, Mobile EngineeringGary has a PhD in Engineering and a Masters in Behavioral Science. He is the old
man of the group (programming Apple products for over 30 years) and his first computer was an Apple II.
• Chris Truman – iOS EngineerSelf taught iOS developer, born and raised in LA. Previously worked at Urbanspoon
and TripAdvisor. Currently mentoring students in Swift through the Thinkful & One Month Swift programs. Loves Dogs, Comic Books, and long walks on the beach.
• Premal Mistry – iOS EngineerPremal loves designing and developing apps for iOS and Android and has over 6
years of experience in Software Development. He has a Masters in Computer Science and is passionate about learning new technologies (Swift, Design Patterns, Animation, etc). During weekends, I love to watch movies, TV shows, hanging out with friends and Yelping various food places in LA.
• Heena Rastogi – Senior iOS EngineerHeena earned a Masters in Computer Science (Multimedia and Creative
Technologies) and has been developing iOS apps for the past 6 years. She loves being a part of development projects that are focused on creative and exciting technologies. Outside of coding, she enjoys reading, running on the beach and listening to electronic music.
The Team
![Page 6: Swift meetup22june2015](https://reader030.fdocuments.net/reader030/viewer/2022032700/55d27f79bb61eb7f078b461f/html5/thumbnails/6.jpg)
• UIStackView• Rich Playgrounds• Slide Over & Split View & PiP Video• Universal Links• App Search & CoreSpotlight• SFSafariViewController• App Transport Security• App Thinning• Storyboard References• UI Testing• Metal & Games• TestFlight and Push Notifications
What You Might Have Missed at WWDC 2015
![Page 7: Swift meetup22june2015](https://reader030.fdocuments.net/reader030/viewer/2022032700/55d27f79bb61eb7f078b461f/html5/thumbnails/7.jpg)
• Axis• Distribution• Spacing• Animatable
UIStackView
![Page 8: Swift meetup22june2015](https://reader030.fdocuments.net/reader030/viewer/2022032700/55d27f79bb61eb7f078b461f/html5/thumbnails/8.jpg)
• Markdown• Multi-Pages• Embedded Resources
Rich Playgrounds
![Page 9: Swift meetup22june2015](https://reader030.fdocuments.net/reader030/viewer/2022032700/55d27f79bb61eb7f078b461f/html5/thumbnails/9.jpg)
• Launch Screen Storyboard• Autolayout & Size Classes• Window vs Screen
Slide Over & Split View & PiP
![Page 10: Swift meetup22june2015](https://reader030.fdocuments.net/reader030/viewer/2022032700/55d27f79bb61eb7f078b461f/html5/thumbnails/10.jpg)
• App Site Association• Similar to Android Default Intents
Universal Links
![Page 11: Swift meetup22june2015](https://reader030.fdocuments.net/reader030/viewer/2022032700/55d27f79bb61eb7f078b461f/html5/thumbnails/11.jpg)
• Background Indexing• NSUserActivity• Web Markup• Meta Data
App Search & CoreSpotlight
![Page 12: Swift meetup22june2015](https://reader030.fdocuments.net/reader030/viewer/2022032700/55d27f79bb61eb7f078b461f/html5/thumbnails/12.jpg)
• Separate process• Cookies & Keychain• Tint Color• Custom Activity Items
SFSafariViewController
![Page 13: Swift meetup22june2015](https://reader030.fdocuments.net/reader030/viewer/2022032700/55d27f79bb61eb7f078b461f/html5/thumbnails/13.jpg)
• HTTPS is mandatory-ish• Info.plist disabling• Exception Domains
App Transport Security
![Page 14: Swift meetup22june2015](https://reader030.fdocuments.net/reader030/viewer/2022032700/55d27f79bb61eb7f078b461f/html5/thumbnails/14.jpg)
• Asset Splitting - @1x, @2x, @3x• On Demand Resources• BitCode
App Thinning
![Page 15: Swift meetup22june2015](https://reader030.fdocuments.net/reader030/viewer/2022032700/55d27f79bb61eb7f078b461f/html5/thumbnails/15.jpg)
• Split up Large Storyboards• Custom Unwind Segues
Storyboard References
![Page 16: Swift meetup22june2015](https://reader030.fdocuments.net/reader030/viewer/2022032700/55d27f79bb61eb7f078b461f/html5/thumbnails/16.jpg)
• New Testing Bundle Type• Record a Script
UI Testing
![Page 17: Swift meetup22june2015](https://reader030.fdocuments.net/reader030/viewer/2022032700/55d27f79bb61eb7f078b461f/html5/thumbnails/17.jpg)
• Core Animation• On the Mac• ReplayKit, GameplayKit, ModelKit
Metal & Games
![Page 18: Swift meetup22june2015](https://reader030.fdocuments.net/reader030/viewer/2022032700/55d27f79bb61eb7f078b461f/html5/thumbnails/18.jpg)
• Tester Limit increased to 2000• Push Notifications Text Input• 4k Push Payload Limit Increase• Feedback Service will be deprecated in
2016
TestFlight & Push Notifications
![Page 19: Swift meetup22june2015](https://reader030.fdocuments.net/reader030/viewer/2022032700/55d27f79bb61eb7f078b461f/html5/thumbnails/19.jpg)
• Checking API Availability• Synthesized Headers• Protocol Extension• Error Handling• And more…
What’s New in Swift 2.0
![Page 20: Swift meetup22june2015](https://reader030.fdocuments.net/reader030/viewer/2022032700/55d27f79bb61eb7f078b461f/html5/thumbnails/20.jpg)
Apple always rolls out new API’s and classes throughout the year.
How do we check for API & Class availability?
Checking API Availability in Swift
![Page 21: Swift meetup22june2015](https://reader030.fdocuments.net/reader030/viewer/2022032700/55d27f79bb61eb7f078b461f/html5/thumbnails/21.jpg)
The old way:if NSClassFromString("SFSafariViewControler") != nil { let eHAdviceVC = SFSafariViewController(URL: url, entersReaderIfAvailable: true) eHAdviceVC.delegate = self presentViewController(eHAdviceVC, animated: true, completion: nil) } else { let eHAdviceVC = eHAdviceViewController(URL: url); presentViewController(eHAdviceVC, animated: true, completion: nil)}
Checking API Availability in Swift
![Page 22: Swift meetup22june2015](https://reader030.fdocuments.net/reader030/viewer/2022032700/55d27f79bb61eb7f078b461f/html5/thumbnails/22.jpg)
Checking API Availability in Swift
The new way:if #available(iOS 9, OS X 10.10, watchOS 2, *) {
let eHAdviceVC = SFSafariViewController(URL: url, entersReaderIfAvailable: true) eHAdviceVC.delegate = self presentViewController(eHAdviceVC, animated: true, completion: nil) } else { let eHAdviceVC = eHAdviceViewController(); presentViewController(eHAdviceVC, animated: true, completion: nil)}
![Page 23: Swift meetup22june2015](https://reader030.fdocuments.net/reader030/viewer/2022032700/55d27f79bb61eb7f078b461f/html5/thumbnails/23.jpg)
Synthesized header files:
Xcode 7 scans through your code and produces virtual header files that summarize the exposed methods with none of the code
Documentation - Similar to Apple's own classes
How to generate synthesized headers in Xcode 7?Xcode 7, go to Navigate > Generated Interface.
Synthesized Headers
![Page 24: Swift meetup22june2015](https://reader030.fdocuments.net/reader030/viewer/2022032700/55d27f79bb61eb7f078b461f/html5/thumbnails/24.jpg)
Synthesized Headersextension NSURLRequest: URLRequestConvertible { public var URLRequest: NSURLRequest}
func URLRequest(method: Method, URL: URLStringConvertible) -> NSURLRequest
/** Creates a request using the shared manager instance for the specified method, URL string, parameters, and parameter encoding.
:param: method The HTTP method. :param: URLString The URL string. :param: parameters The parameters. `nil` by default. :param: encoding The parameter encoding. `.URL` by default.
:returns: The created request.*/public func request(method: Method, URLString: URLStringConvertible, parameters: [String: AnyObject]? = nil, encoding: ParameterEncoding = .URL) -> Request
/** Creates a request using the shared manager instance for the specified URL request.
If `startRequestsImmediately` is `true`, the request will have `resume()` called before being returned.
:param: URLRequest The URL request
:returns: The created request.*
![Page 25: Swift meetup22june2015](https://reader030.fdocuments.net/reader030/viewer/2022032700/55d27f79bb61eb7f078b461f/html5/thumbnails/25.jpg)
Protocol Extensions
Protocols can be extended to provide method and property implementations to conforming types.
You can use protocol extensions to provide a default implementation to any method or property requirement of that protocol.
![Page 26: Swift meetup22june2015](https://reader030.fdocuments.net/reader030/viewer/2022032700/55d27f79bb61eb7f078b461f/html5/thumbnails/26.jpg)
Protocol ExtensionsExample:
let ageOfEmployees: [Int?] = [30, 32, nil, 45, 50, 12, 8, 25, nil]
extension SequenceType where Generator.Element: OptionalType { var purify: [Self.Generator.Element.T] { return self.map { $0.optional }.filter { $0 != nil }.map { $0! } }}
let ageOfEmployees: [Int?] = [30, 32, nil, 45, 50, 12, 8, 25, nil]ageOfEmployees.purify
[30, 32, 45, 50, 12, 8, 25]
![Page 27: Swift meetup22june2015](https://reader030.fdocuments.net/reader030/viewer/2022032700/55d27f79bb61eb7f078b461f/html5/thumbnails/27.jpg)
Error Handling
Example:func refreshActivityFeed() { let refreshToken = login() loadActivityFeed(refreshToken) updateUI()}
![Page 28: Swift meetup22june2015](https://reader030.fdocuments.net/reader030/viewer/2022032700/55d27f79bb61eb7f078b461f/html5/thumbnails/28.jpg)
Error HandlingThe Old Way:func refreshActivityFeed() { let refreshToken:String? = login(username, error:&error) if (error != nil) { if let error = error { switch (error.code) { case ERROR_UNAUTHORIZED: showLoginVC() case ERROR_FORBIDDEN: showErrorAlert() case ERROR_NOT_FOUND: showLoginVC() default: showErrorAlert() } } } else { // Fetch the most recent activity feeds from the server // Parse and prepare data structure // Update UI loadActivityFeed(refreshToken!) updateUI() }}
![Page 29: Swift meetup22june2015](https://reader030.fdocuments.net/reader030/viewer/2022032700/55d27f79bb61eb7f078b461f/html5/thumbnails/29.jpg)
The New Way:func refreshActivityFeed() { do { let refreshToken = try login(username) loadActivityFeed(refreshToken) updateUI() } catch (LoginError.ERROR_UNAUTHORIZED) { showLoginVC() } catch (LoginError.ERROR_FORBIDDEN) { showErrorAlert() } catch (LoginError.ERROR_NOT_FOUND) { showLoginVC() } catch { showErrorAlert() }}
Error Handling
![Page 30: Swift meetup22june2015](https://reader030.fdocuments.net/reader030/viewer/2022032700/55d27f79bb61eb7f078b461f/html5/thumbnails/30.jpg)
Error HandlingRepresenting Errors
• ErrorType is a protocol in the Swift Standard Library
• Any type that conforms to ErrorType can be thrown and caught
• Can make your own types conform as well
• enum is great for groups of related errors
enum LoginError : ErrorType { case ERROR_UNAUTHORIZED case ERROR_FORBIDDEN case ERROR_NOT_FOUND case ERROR_NOT_ALLOWED case ErrorWithMessage(code: Int, message: String)}
![Page 31: Swift meetup22june2015](https://reader030.fdocuments.net/reader030/viewer/2022032700/55d27f79bb61eb7f078b461f/html5/thumbnails/31.jpg)
Throwing Errors:enum VendingMachineError: ErrorType { case InvalidSelection case InsufficientFunds(required: Double) case OutOfStock}
func purchaseItemFromVendingMachine(item: VendingItem) throws -> Bool { if item.count() > 0 { // ..... return true } else { throw VendingMachineError.OutOfStock }}
Error Handling
![Page 32: Swift meetup22june2015](https://reader030.fdocuments.net/reader030/viewer/2022032700/55d27f79bb61eb7f078b461f/html5/thumbnails/32.jpg)
Catching and Handling Errors:func refreshActivityFeed() { do { let refreshToken = try loginWithUserName(username) loadActivityFeed(refreshToken) updateUI() } catch (LoginError.ERROR_UNAUTHORIZED) { showLoginVC() } catch (LoginError.ERROR_FORBIDDEN) { showErrorAlert() } catch (LoginError.ERROR_NOT_FOUND) { showLoginVC() } catch { showErrorAlert() }}
Error Handling
![Page 33: Swift meetup22june2015](https://reader030.fdocuments.net/reader030/viewer/2022032700/55d27f79bb61eb7f078b461f/html5/thumbnails/33.jpg)
Disabling Error Propagation:
try! loadConfiguration()
Error Handling
![Page 34: Swift meetup22june2015](https://reader030.fdocuments.net/reader030/viewer/2022032700/55d27f79bb61eb7f078b461f/html5/thumbnails/34.jpg)
Specifying Cleanup Actions:func processFile(filename: String) throws { if exists(filename) {
do { let file = try open(filename) defer { close(file) }
while let line = try file.readline() { ... }
} catch {...
} }}
Error Handling
![Page 35: Swift meetup22june2015](https://reader030.fdocuments.net/reader030/viewer/2022032700/55d27f79bb61eb7f078b461f/html5/thumbnails/35.jpg)
And More…Guard:func validateJSON() -> Bool { guard let name = person["name"] else { return false } ..... ..... return true}
• Designed to solve pyramid of doom problem• Extremely useful for early exit
![Page 36: Swift meetup22june2015](https://reader030.fdocuments.net/reader030/viewer/2022032700/55d27f79bb61eb7f078b461f/html5/thumbnails/36.jpg)
Swift & Enterprise & eHarmony
• Why?• Bridging & Migration• eHarmony’s Plan to Tackle
![Page 37: Swift meetup22june2015](https://reader030.fdocuments.net/reader030/viewer/2022032700/55d27f79bb61eb7f078b461f/html5/thumbnails/37.jpg)
• 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?
![Page 38: Swift meetup22june2015](https://reader030.fdocuments.net/reader030/viewer/2022032700/55d27f79bb61eb7f078b461f/html5/thumbnails/38.jpg)
3 important aspects of Swift / Objective-C compatibility:
• InteroperabilityAbility to interface between Swift and Objective-C in either direction
• Mix and MatchAllows you to create mixed-language apps containing both Swift and Objective-C files that can communicate with each other
• MigrationMigration 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
![Page 39: Swift meetup22june2015](https://reader030.fdocuments.net/reader030/viewer/2022032700/55d27f79bb61eb7f078b461f/html5/thumbnails/39.jpg)
3 New features introduced for Objective-C :
• Nullability for Objective-C
• Lightweight Generics
• Kind of
Bridging & Migration
![Page 40: Swift meetup22june2015](https://reader030.fdocuments.net/reader030/viewer/2022032700/55d27f79bb61eb7f078b461f/html5/thumbnails/40.jpg)
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}
![Page 41: Swift meetup22june2015](https://reader030.fdocuments.net/reader030/viewer/2022032700/55d27f79bb61eb7f078b461f/html5/thumbnails/41.jpg)
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
![Page 42: Swift meetup22june2015](https://reader030.fdocuments.net/reader030/viewer/2022032700/55d27f79bb61eb7f078b461f/html5/thumbnails/42.jpg)
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.
![Page 43: Swift meetup22june2015](https://reader030.fdocuments.net/reader030/viewer/2022032700/55d27f79bb61eb7f078b461f/html5/thumbnails/43.jpg)
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
![Page 44: Swift meetup22june2015](https://reader030.fdocuments.net/reader030/viewer/2022032700/55d27f79bb61eb7f078b461f/html5/thumbnails/44.jpg)
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
![Page 45: Swift meetup22june2015](https://reader030.fdocuments.net/reader030/viewer/2022032700/55d27f79bb61eb7f078b461f/html5/thumbnails/45.jpg)
Objective–C@interface SNGFeedView@property(nonatomic)NSArray <UIView*> feedSubviews;@property(nonatomic,assign)BOOL filter;@end
Swiftclass SNGFeedView {var subviews: [UIView] { get }}
Lightweight Generics
![Page 46: Swift meetup22june2015](https://reader030.fdocuments.net/reader030/viewer/2022032700/55d27f79bb61eb7f078b461f/html5/thumbnails/46.jpg)
• It tells compiler it’s object of some kind of of given type
extern __kindof NSApplication *NSApp; // NSApplication instanceNSObject *object = NSApp; // convert to super classMyApplication *myApp = NSApp; // implicit downcast subclassNSString *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
![Page 47: Swift meetup22june2015](https://reader030.fdocuments.net/reader030/viewer/2022032700/55d27f79bb61eb7f078b461f/html5/thumbnails/47.jpg)
• Modernization of Objective-C
• Generating Bridging Headers
• Incremental Migration
eHarmony’s Plan to Tackle
![Page 48: Swift meetup22june2015](https://reader030.fdocuments.net/reader030/viewer/2022032700/55d27f79bb61eb7f078b461f/html5/thumbnails/48.jpg)
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
![Page 49: Swift meetup22june2015](https://reader030.fdocuments.net/reader030/viewer/2022032700/55d27f79bb61eb7f078b461f/html5/thumbnails/49.jpg)
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
![Page 50: Swift meetup22june2015](https://reader030.fdocuments.net/reader030/viewer/2022032700/55d27f79bb61eb7f078b461f/html5/thumbnails/50.jpg)
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
![Page 51: Swift meetup22june2015](https://reader030.fdocuments.net/reader030/viewer/2022032700/55d27f79bb61eb7f078b461f/html5/thumbnails/51.jpg)
Questions?