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

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

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

Taiki Suzuki

August 29, 2016
Tweet

More Decks by Taiki Suzuki

Other Decks in Programming

Transcript

  1. 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"
  2. 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
  3. 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
  4. Implicitly Unwrapped Optional (҉໧తΞϯϥοϓܕ) and AnyObject // In Objective-C header

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

    (instancetype __nullable)initWithPathString:(NSString * __nonnull)pathString parameters:(NSArray<NSString *> * __nullable)parameters;
  7. TARGETS -> Build Setting -> Swift Compiler - Code Generation

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

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

    *.m) #import "ObjcSwiftSampleHoge-Swift.h"
  10. 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
  11. 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
  12. 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
  13. Swift Enum enum PostType { case Text case Image case

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

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

    Int? let postType: PostType let elements: [BodyElement] let createdDate: NSDate let updatedDate: NSDate }
  16. 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) { //... } }
  17. 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) }
  18. 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) { //... } }
  19. 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) { //... } }
  20. 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() }
  21. Unreadable from Objective-C struct Post { let postId: Int let

    senderId: Int? let postType: PostType let elements: [BodyElement] let createdDate: NSDate let updatedDate: NSDate }
  22. 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() } }
  23. 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 } //... } }
  24. 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...
  25. Ordinary Code for Swift NSLayoutConstraint(item: view, attribute: .Top, relatedBy: .Equal,

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

    view.Top |+| 10, view.Right |-| 10, view.Left |+| 10, view.Bottom |-| 10 )
  27. 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...
  28. 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) ]];
  29. 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)