Slide 1

Slide 1 text

Objective-CͷϓϩδΣΫτΛ ϦχϡʔΞϧΛػʹ SwiftҠߦ࢝͠Ίͨ࿩ marty-suzuki

Slide 2

Slide 2 text

ࣗݾ঺հ Github: https://github.com/marty- suzuki Twitter: https://twitter.com/ marty_suzuki

Slide 3

Slide 3 text

SAHistoryNavigationVie wController https://github.com/marty-suzuki/ SAHistoryNavigationViewController 1,272 stars

Slide 4

Slide 4 text

URLEmbeddedView https://github.com/marty-suzuki/ URLEmbeddedView 296 stars

Slide 5

Slide 5 text

Objective-C͔ΒSwift΁ͷ Ҡߦɺߦͬͯ·͔͢ʁ

Slide 6

Slide 6 text

Swift͕ศརա͗ͯɺ Objective-C͔Β໨Λഎ͚ ͍ͯ·ͤΜ͔ʁ

Slide 7

Slide 7 text

Loading Objective-C from Swift

Slide 8

Slide 8 text

TARGETS -> Build Setting -> Swift Compiler - Code Generation -> Objective-C Bridging Header

Slide 9

Slide 9 text

Objective-C Bridging Header // // Use this file to import your target's public headers that // you would like to expose to Swift. // #import "TNPost.h" #import "TNBodyElement.h"

Slide 10

Slide 10 text

enum // Definition in Objective-C typedef enum : int32_t { TNPostType_Text, TNPostType_Image, TNPostType_Text_Image, TNPostType_Video, TNPostType_Text_Video, } TNPostType; // Use in Swift let postType: TNPostType = TNPostType_Image TNPostType_Image.rawValue

Slide 11

Slide 11 text

NS_ENUM // Definition in Objective-C typedef NS_ENUM(NSInteger, TNPostType) { TNPostType_Text, TNPostType_Image, TNPostType_Text_Image, TNPostType_Video, TNPostType_Text_Video, }; // Use in Swift let postType: TNPostType = .Image TNPostType.Image.rawValue

Slide 12

Slide 12 text

Variable arguments (Մม௕Ҿ਺) // In Objective-C header file - (id)initWithPathString:(NSString *)pathString parameter:(NSString *)firstParameter, ...;

Slide 13

Slide 13 text

Swift readable definition // In Objective-C header file - (id)initWithPathString:(NSString *)pathString parameters:(NSArray *)parameters;

Slide 14

Slide 14 text

Implicitly Unwrapped Optional (҉໧తΞϯϥοϓܕ) and AnyObject // In Objective-C header file - (id)initWithPathString:(NSString *)pathString parameters:(NSArray *)parameters;

Slide 15

Slide 15 text

Original definition // In Objective-C header file - (id)initWithPathString:(NSString *)pathString parameters:(NSArray *)parameters; Improved definition // In Objective-C header file - (instancetype __nullable)initWithPathString:(NSString * __nonnull)pathString parameters:(NSArray * __nullable)parameters;

Slide 16

Slide 16 text

Optional and Correct type // In Objective-C header file - (instancetype __nullable)initWithPathString:(NSString * __nonnull)pathString parameters:(NSArray * __nullable)parameters;

Slide 17

Slide 17 text

Loading Swift from Objective-C

Slide 18

Slide 18 text

Can you make it for Objective-C? — Objective-C൛࡞ͬͯ΋Β͑·͔͢ʁ

Slide 19

Slide 19 text

TARGETS -> Build Setting -> Swift Compiler - Code Generation -> Objective-C Generated Interface Header Name

Slide 20

Slide 20 text

TARGETS -> Build Setting -> Packaging -> Product Module Name // In Objective-C files (*.h or *.m) #import "ObjcSwiftSample-Swift.h"

Slide 21

Slide 21 text

Change Product Module Name // In Objective-C files (*.h or *.m) #import "ObjcSwiftSampleHoge-Swift.h"

Slide 22

Slide 22 text

Circular reference ObjcSwiftSample-Bridging-Header.h // // Use this file to import your target's public headers that // you would like to expose to Swift. // #import "TNPost.h" TNPost.h #import "ObjcSwiftSample-Swift.h" //Error @interface TNPost: NSObject @end

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

TNPost.h typedef NS_ENUM(int32_t, TNPostType) { TNPostType_Text, TNPostType_Image, TNPostType_Text_Image, TNPostType_Video, TNPostType_Text_Video, }; @class TNBodyElement; @interface TNPost : NSObject @property (assign, nonatomic) NSInteger postId; @property (assign, nonatomic) NSInteger senderId; @property (assign, nonatomic) TNPostType postType; @property (copy, nonatomic) NSArray *elements; @property (copy, nonatomic) NSDate *createdDate; @property (copy, nonatomic) NSDate *updatedDate; @end

Slide 25

Slide 25 text

TNBodyElement.h typedef NS_ENUM(NSInteger, TNBodyType) { TNBodyType_Text, TNBodyType_Image, TNBodyType_Video }; @interface TNBodyElement : NSObject @property (assign, nonatomic) TNBodyType bodyType; //type text @property (copy, nonatomic) NSString *text; //type image & video @property (copy, nonatomic) NSString *thumbnailUrl; @property (assign, nonatomic) CGFloat width; @property (assign, nonatomic) CGFloat height; //type image @property (copy, nonatomic) NSString *rawImageUrl; //type video @property (copy, nonatomic) NSString *videoUrl; @end

Slide 26

Slide 26 text

Swift Enum enum PostType { case Text case Image case Text_Image case Video case Text_Video }

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

Objective-C readable enum @objc enum PostType: Int { case Text case Image case Text_Image case Video case Text_Video }

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

Swift class class Post { let postId: Int let senderId: Int? let postType: PostType let elements: [BodyElement] let createdDate: NSDate let updatedDate: NSDate }

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

class Post: NSObject { let postId: Int let senderId: Int? let postType: PostType let elements: [BodyElement] let createdDate: NSDate let updatedDate: NSDate init(postId: Int, senderId: Int?, postType: PostType, elements: [BodyElement], createdDate: NSDate, updatedDate: NSDate) { //... } }

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

Associated Values enum BodyElement { case Text (text : String) case Image(thumbnail: String, width: CGFloat, height: CGFloat, rawImageUrl: String) case Video(thumbnail: String, width: CGFloat, height: CGFloat, videoUrl: String) }

Slide 35

Slide 35 text

class Post: NSObject { let postId: Int let senderId: Int? let postType: PostType let elements: [BodyElement] let createdDate: NSDate let updatedDate: NSDate init(postId: Int, senderId: Int?, // can not recognize from Objective-C postType: PostType, elements: [BodyElement], // can not recognize from Objective-C createdDate: NSDate, updatedDate: NSDate) { //... } }

Slide 36

Slide 36 text

class Post: NSObject { let postId: Int let senderId: Int? let postType: PostType let elements: [BodyElement] let createdDate: NSDate let updatedDate: NSDate init(postId: Int, senderId: NSNumber?, postType: PostType, elements: [TNBodyElement], createdDate: NSDate, updatedDate: NSDate) { //... } }

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

extension TNBodyElement { func toBodyElement() -> BodyElement? { switch bodyType { case .Text: guard let text = text else { return nil } return .Text(text: text) case .Image: guard let thumbnailUrl = thumbnailUrl, rawImageUrl = rawImageUrl else { return nil } return .Image(thumbnail: thumbnailUrl, width: width, height: height, rawImageUrl: rawImageUrl) case .Video: guard let thumbnailUrl = thumbnailUrl, videoUrl = videoUrl else { return nil } return .Video(thumbnail: thumbnailUrl, width: width, height: height, videoUrl: videoUrl) } } } // rawElements: [TNBodyElement] let elements: [BodyElement] = rawElements.flatMap { $0.toBodyElement() }

Slide 39

Slide 39 text

BobyElement [ .Text(text: "ςΩετதͷը૾"), .Image(...), .Text(text: "͕ɺೖͬͨΓɺಠࣗͷֆจࣈ"), .Image(...), .Text(text: "͕ೖͬͨΓ͢Δ") ]

Slide 40

Slide 40 text

Unreadable from Objective-C struct Post { let postId: Int let senderId: Int? let postType: PostType let elements: [BodyElement] let createdDate: NSDate let updatedDate: NSDate }

Slide 41

Slide 41 text

Delegate protocol ViewControllerDelegate { func viewController(viewController: ViewController, didSendPost post: Post) }

Slide 42

Slide 42 text

Delegate @objc protocol ViewControllerDelegate { func viewController(viewController: ViewController, didSendPost post: Post) }

Slide 43

Slide 43 text

Wrapper class BodyElementWrapper: NSObject { class func text(text: String) -> BodyElementWrapper { return BodyElementWrapper(bodyElement: .Text(text: text)) } //... let bodyElement: BodyElement init(bodyElement: BodyElement) { self.bodyElement = bodyElement super.init() } }

Slide 44

Slide 44 text

Wrapper

Slide 45

Slide 45 text

Wrapper class Post: NSObject { let postId: Int let senderId: Int? let postType: PostType let elements: [BodyElement] let createdDate: NSDate let updatedDate: NSDate init(postId: Int, senderId: NSNumber?, postType: PostType, elements: [BodyElementWrapper], createdDate: NSDate, updatedDate: NSDate) { //... self.elements = elements.map { $0.bodyElement } //... } }

Slide 46

Slide 46 text

755 Layout pattern

Slide 47

Slide 47 text

755 Layout pattern

Slide 48

Slide 48 text

755 Parts

Slide 49

Slide 49 text

MisterFusion https://github.com/marty-suzuki/MisterFusion 214 stars

Slide 50

Slide 50 text

Ordinary Code for Swift let view = UIView() self.view.addSubview(view) view.translatesAutoresizingMaskIntoConstraints = false self.view.addConstraints([ NSLayoutConstraint(item: view, attribute: .Top, relatedBy: .Equal, toItem: self.view, attribute: .Top, multiplier: 1, constant: 10), NSLayoutConstraint(item: view, attribute: .Right, relatedBy: .Equal, toItem: self.view, attribute: .Right, multiplier: 1, constant: -10), NSLayoutConstraint(item: view, attribute: .Left, relatedBy: .Equal, toItem: self.view, attribute: .Left, multiplier: 1, constant: 10), NSLayoutConstraint(item: view, attribute: .Bottom, relatedBy: .Equal, toItem: self.view, attribute: .Bottom, multiplier: 1, constant: -10), ]) Too long...

Slide 51

Slide 51 text

Ordinary Code for Swift NSLayoutConstraint(item: view, attribute: .Top, relatedBy: .Equal, toItem: self.view, attribute: .Top, multiplier: 1, constant: 10)

Slide 52

Slide 52 text

MisterFusion Code for Swift let view = UIView() self.view.addLayoutSubview(view, andConstraints: view.Top |+| 10, view.Right |-| 10, view.Left |+| 10, view.Bottom |-| 10 )

Slide 53

Slide 53 text

Edges zero let view = UIView() self.view.addLayoutSubview(view, andConstraints: view.Top, view.Right, view.Left, view.Bottom )

Slide 54

Slide 54 text

Ordinary Code for Objective-C UIView *view = [UIView new]; view.translatesAutoresizingMaskIntoConstraints = NO; [self.view addSubview: view]; [self.view addConstraints:@[ [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0f constant:10.0f], [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0f constant:-10.0f], [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0f constant:10.0f], [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeHeight multiplier:0.5f constant:-15.0f] ]]; I can't see... too long...

Slide 55

Slide 55 text

MisterFusion Code for Objective-C #import UIView *view = [UIView new]; [self.view addLayoutSubview:view andConstraints:@[ view.Top .Constant(10.0f), view.Right .Constant(-10.0f), view.Left .Constant(10.0f), view.Bottom.Constant(-10.0f) ]];

Slide 56

Slide 56 text

let hogeView = UIView() let misterFusions: [MisterFusion] if let imageView = imageView { misterFusions = [ hogeView.Right |==| imageView.Left |+| 4, hogeView.Left |-| 4, ] } else { misterFusions = [ hogeView.Right |+| 8, hogeView.Left |-| 8, ] } let commonMisterFusion = [ hogeView.Top, hogeView.Bottom ] let combinedMisterFusions = Array([misterFusions, commonMisterFusion].flatten()) self.view.addLayoutSubview(hogeView, andConstraints: combinedMisterFusions)

Slide 57

Slide 57 text

Objective-C͔Β Swift΁ͷҠߦ ࢝ΊͯΈ·ͤΜ͔ʁ

Slide 58

Slide 58 text

͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠ marty-suzuki