Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Objective-Cのプロジェクトをリニューアルを機にSwift移行し始めた話

 Objective-Cのプロジェクトをリニューアルを機にSwift移行し始めた話

Ae276805027a01983503c3edafbdb6b2?s=128

Taiki Suzuki

August 29, 2016
Tweet

More Decks by Taiki Suzuki

Other Decks in Programming

Transcript

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

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

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

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

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

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

  7. Loading Objective-C from Swift

  8. TARGETS -> Build Setting -> Swift Compiler - Code Generation

    -> Objective-C Bridging Header
  9. 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"
  10. 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
  11. 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
  12. Variable arguments (Մม௕Ҿ਺) // In Objective-C header file - (id)initWithPathString:(NSString

    *)pathString parameter:(NSString *)firstParameter, ...;
  13. Swift readable definition // In Objective-C header file - (id)initWithPathString:(NSString

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

    file - (id)initWithPathString:(NSString *)pathString parameters:(NSArray *)parameters;
  15. 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<NSString *> * __nullable)parameters;
  16. Optional and Correct type // In Objective-C header file -

    (instancetype __nullable)initWithPathString:(NSString * __nonnull)pathString parameters:(NSArray<NSString *> * __nullable)parameters;
  17. Loading Swift from Objective-C

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

  19. TARGETS -> Build Setting -> Swift Compiler - Code Generation

    -> Objective-C Generated Interface Header Name
  20. TARGETS -> Build Setting -> Packaging -> Product Module Name

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

    *.m) #import "ObjcSwiftSampleHoge-Swift.h"
  22. 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
  23. None
  24. 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<TNBodyElement *> *elements; @property (copy, nonatomic) NSDate *createdDate; @property (copy, nonatomic) NSDate *updatedDate; @end
  25. 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
  26. Swift Enum enum PostType { case Text case Image case

    Text_Image case Video case Text_Video }
  27. None
  28. Objective-C readable enum @objc enum PostType: Int { case Text

    case Image case Text_Image case Video case Text_Video }
  29. None
  30. Swift class class Post { let postId: Int let senderId:

    Int? let postType: PostType let elements: [BodyElement] let createdDate: NSDate let updatedDate: NSDate }
  31. None
  32. 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) { //... } }
  33. None
  34. 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) }
  35. 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) { //... } }
  36. 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) { //... } }
  37. None
  38. 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() }
  39. BobyElement [ .Text(text: "ςΩετதͷը૾"), .Image(...), .Text(text: "͕ɺೖͬͨΓɺಠࣗͷֆจࣈ"), .Image(...), .Text(text: "͕ೖͬͨΓ͢Δ")

    ]
  40. Unreadable from Objective-C struct Post { let postId: Int let

    senderId: Int? let postType: PostType let elements: [BodyElement] let createdDate: NSDate let updatedDate: NSDate }
  41. Delegate protocol ViewControllerDelegate { func viewController(viewController: ViewController, didSendPost post: Post)

    }
  42. Delegate @objc protocol ViewControllerDelegate { func viewController(viewController: ViewController, didSendPost post:

    Post) }
  43. 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() } }
  44. Wrapper

  45. 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 } //... } }
  46. 755 Layout pattern

  47. 755 Layout pattern

  48. 755 Parts

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

  50. 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...
  51. Ordinary Code for Swift NSLayoutConstraint(item: view, attribute: .Top, relatedBy: .Equal,

    toItem: self.view, attribute: .Top, multiplier: 1, constant: 10)
  52. MisterFusion Code for Swift let view = UIView() self.view.addLayoutSubview(view, andConstraints:

    view.Top |+| 10, view.Right |-| 10, view.Left |+| 10, view.Bottom |-| 10 )
  53. Edges zero let view = UIView() self.view.addLayoutSubview(view, andConstraints: view.Top, view.Right,

    view.Left, view.Bottom )
  54. 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...
  55. MisterFusion Code for Objective-C #import <MisterFusion/MisterFusion-Swift.h> 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) ]];
  56. 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)
  57. Objective-C͔Β Swift΁ͷҠߦ ࢝ΊͯΈ·ͤΜ͔ʁ

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