Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Mixing Objective-C, Swift and Japan

1,574 views

Published on

Oct 20, 2018 iPlayground@NTU

Published in: Engineering
  • Be the first to comment

Mixing Objective-C, Swift and Japan

  1. 1. Mixing ObjC, Swift and Japan Tina Chang LINE Fukuoka
  2. 2. INTRODUCTION 旅⽇日台灣⼯工程師 吃貨 🥞 iOS歷3年年 🐣 RUN FOR EAT
  3. 3. ‣ MIXING OVERVIEW ‣ CIRCULAR REFERENCE ‣ JAPAN
  4. 4. ‣ JAPAN ‣ MIXING OVERVIEW ‣ CIRCULAR REFERENCE #
  5. 5. JAPAN ENGINEER SALARY?
  6. 6. AMAZON / GOOGLE / INDEED TIER1 850万円↑
  7. 7. LINE / MERCARI / RAKUTEN TIER2 600-900万円
  8. 8. OTHERS 400-700万円
  9. 9. HOW MUCH YOU CAN SAVE?
  10. 10. ✓ 所得稅 ✓ 健康保險料 ✓ 厚⽣生年年⾦金金保險
  11. 11. 薪⽔水900万円↓ 80%
  12. 12. 薪⽔水900万円↑ 70%
  13. 13. ✓ 房租 ✓ ⽔水電瓦斯費 ✓ 飲食費 ✓ 交通費
  14. 14. ✓ 房租      ✓ ⽔水電瓦斯費   ✓ 飲食費 ✓ 交通費 8万円 1万円 6万円 0円 15万円
  15. 15. ✓ 房租      ✓ ⽔水電瓦斯費   ✓ 飲食費 ✓ 交通費 8万円 1万円 6万円 0円 15万円
  16. 16. 2000円 420円 780円 680円
  17. 17. 1000円 180円 430円 400円
  18. 18. ✓ 房租      ✓ ⽔水電瓦斯費   ✓ 飲食費 ✓ 交通費 8万円 1万円 6万円 0円 15万円
  19. 19. 薪⽔水 600万円
  20. 20. 600万円 x 80% - 15万 x 12 300万円
  21. 21. ‣ JAPAN ‣ MIXING OVERVIEW ‣ CIRCULAR REFERENCE #
  22. 22. GOOD POINTS FOR MIXING?
  23. 23. WE CAN WRITE SWIFT
  24. 24. Bridging Header Generated Header
  25. 25. Bridging Header Generated Header Swift → Objective-C
  26. 26. Bridging Header Generated Header Objective-C → Swift
  27. 27. Bridging Header Generated Header Swift → Objective-CObjective-C → Swift Mixed-Proj Mixed-Proj-Bridging-Header.h Mixed_Proj-Swift.h
  28. 28. Swift → Objective-C
  29. 29. @objc (LINESticker) class Sticker: NSObject { } // StickerViewController.m - (instancetype)initWithSticker:(LINESticker*)sticker { self = [super init]; if (self) { _sticker = sticker; } return self; } Swift Objective-C Swift → Objective-C
  30. 30. @objc (LINESticker) class Sticker: NSObject { } // StickerViewController.m #import “Mixed_Proj-Swift.h” - (instancetype)initWithSticker:(LINESticker*)sticker { self = [super init]; if (self) { _sticker = sticker; } return self; } Swift Objective-C Swift → Objective-C
  31. 31. Objective-C → Swift
  32. 32. class StickerListViewController: UIViewController { func presentStickerViewController() { let stickerViewController = StickerViewController() present(stickerInfoViewController, animated: true, completion: nil) } } // StickerViewController.h @interface StickerViewController : UIViewController @property (nonatomic, strong, nonnull) LINESticker *sticker; @end Swift Objective-C Objective-C → Swift
  33. 33. class StickerListViewController: UIViewController { func presentStickerViewController() { let stickerViewController = StickerViewController() present(stickerInfoViewController, animated: true, completion: nil) } } // StickerViewController.h @interface StickerViewController : UIViewController @end Swift Objective-C Objective-C → Swift // Mixed_Proj-Bridging-Header.h #import "StickerViewController.h"
  34. 34. Bridging Header Generated Header Swift → Objective-CObjective-C → Swift Mixed-Proj Mixed-Proj-Bridging-Header.h Mixed_Proj-Swift.h
  35. 35. ‣ JAPAN ‣ MIXING OVERVIEW ‣ CIRCULAR REFERENCE #
  36. 36. @objc (LINESticker) class Sticker: NSObject { } // Mixed_Proj-Bridging-Header.h #import "StickerViewController.h" // StickerViewController.h #import “Mixed_Proj-Swift.h" @interface StickerViewController : UIViewController @property (nonatomic, strong, nonnull) LINESticker *sticker; @end Swift Objective-C CIRCULAR REFERENCE
  37. 37. @objc (LINESticker) class Sticker: NSObject { } // Mixed_Proj-Bridging-Header.h #import "StickerViewController.h" // StickerViewController.h #import “Mixed_Proj-Swift.h" @interface StickerViewController : UIViewController @property (nonatomic, strong, nonnull) LINESticker *sticker; @end Swift Objective-C ‘Mixed_Proj-Swift.h’ file not found CIRCULAR REFERENCE
  38. 38. ‣ compile Swift module ‣ precompile Bridging Header file (Mixed-Proj-Bridging-Header.h) ‣ compile Swift source files ‣ Generated Header file (Mixed_Proj-Swift.h) ‣ compile Objective-C source files BUILD PROCESS
  39. 39. Bridging Header Generated Header ‣ Swift module ‣ Bridging Header file ‣ Swift source files ‣ Generated Header file ‣ Objective-C source files
  40. 40. Bridging Header Generated Header // Mixed_Proj-Bridging-Header.h #import "StickerViewController.h" ‣ Swift module ‣ Bridging Header file ‣ Swift source files ‣ Generated Header file ‣ Objective-C source files
  41. 41. Bridging Header Generated Header // Mixed_Proj-Bridging-Header.h #import "StickerViewController.h" // StickerViewController.h #import “Mixed_Proj-Swift.h" @interface StickerViewController : UIViewController @property (nonatomic, strong, nonnull) LINESticker *sticker; @end ‣ Swift module ‣ Bridging Header file ‣ Swift source files ‣ Generated Header file ‣ Objective-C source files
  42. 42. Bridging Header Generated Header ‣ Swift module ‣ Bridging Header file ‣ Swift source files ‣ Generated Header file ‣ Objective-C source files // Mixed_Proj-Bridging-Header.h #import "StickerViewController.h" // StickerViewController.h #import “Mixed_Proj-Swift.h" @interface StickerViewController : UIViewController @property (nonatomic, strong, nonnull) LINESticker *sticker; @end
  43. 43. Bridging Header Generated Header ‣ Swift module ‣ Bridging Header file ‣ Swift source files ‣ Generated Header file ‣ Objective-C source files // Mixed_Proj-Bridging-Header.h #import "StickerViewController.h" // StickerViewController.h #import “Mixed_Proj-Swift.h" @interface StickerViewController : UIViewController @property (nonatomic, strong, nonnull) LINESticker *sticker; @end ‘Mixed_Proj-Swift.h’ file not found
  44. 44. @objc (LINESticker) class Sticker: NSObject { } // Mixed_Proj-Bridging-Header.h #import "StickerViewController.h" // StickerViewController.h #import “Mixed_Proj-Swift.h" @interface StickerViewController : UIViewController @property (nonatomic, strong, nonnull) LINESticker *sticker; @end Swift Objective-C ‘Mixed_Proj-Swift.h’ file not found CIRCULAR REFERENCE
  45. 45. FORWARD DECLARATION @objc (LINESticker) class Sticker: NSObject { } // Mixed_Proj-Bridging-Header.h #import "StickerViewController.h" // StickerViewController.h @class LINESticker; @interface StickerViewController : UIViewController @property (nonatomic, strong, nonnull) LINESticker *sticker; @end Swift Objective-C
  46. 46. HOW ABOUT PROTOCOL?
  47. 47. @objc (LINEProduct) protocol Product: NSObjectProtocol { } // Mixed_Proj-Bridging-Header.h #import "ProductViewController.h" // ProductViewController.h @protocol LINEProduct; @interface ProductViewController : UIViewController @property (nonatomic, strong, nonnull) id<LINEProduct> product; @end Swift Objective-C CIRCULAR REFERENCE - PROTOCOL
  48. 48. CONFORM PROTOCOL?
  49. 49. @objc (LINEProduct) protocol Product: NSObjectProtocol { } // Mixed_Proj-Bridging-Header.h #import “Theme.h” // Theme.h @protocol LINEProduct; @interface Theme : NSObject <LINEProduct> @end Swift Objective-C CONFORM PROTOCOL
  50. 50. @objc (LINEProduct) protocol Product: NSObjectProtocol { } // Mixed_Proj-Bridging-Header.h #import “Theme.h” // Theme.h @protocol LINEProduct; @interface Theme : NSObject <LINEProduct> @end Swift Objective-C Cannot find protocol definition for 'LINEProduct' CONFORM PROTOCOL
  51. 51. SOLUTION ✓ REWRITE PROTOCOLS IN OBJC ✓ RETURN SELF AS PROTOCOL TYPE
  52. 52. // Theme.h @protocol LINEProduct; @interface Theme : NSObject - (id<LINEProduct>)theme; @end Objective-C // Theme.m #import "Theme.h" #import “Mixed_Proj-Swift.h” @interface Theme() <LINEProduct> @end @implementation Theme - (id<LINEProduct>)theme { return self; } @end CONFORM PROTOCOL
  53. 53. Swift class used in ObjC class used in Swift
  54. 54. @objc (LINESticker) class Sticker: NSObject { } // Mixed_Proj-Bridging-Header.h #import "StickerViewController.h" // StickerViewController.h @class LINESticker; @interface StickerViewController : UIViewController @property (nonatomic, strong, nonnull) LINESticker *sticker; @end Swift Objective-C SWIFT→OBJC→SWIFT
  55. 55. class StickerListViewController: UIViewController { func presentStickerViewController() { let stickerViewController = StickerViewController() stickerViewController.sticker = Sticker() present(stickerViewController, animated: true, completion: nil) } } SWIFT→OBJC→SWIFT
  56. 56. class StickerListViewController: UIViewController { func presentStickerViewController() { let stickerViewController = StickerViewController() stickerViewController.sticker = Sticker() present(stickerViewController, animated: true, completion: nil) } } Value of type 'StickerViewController' has no member 'sticker' SWIFT→OBJC→SWIFT
  57. 57. @objc (LINESticker) class Sticker: NSObject { } // Mix_Proj-Swift.h SWIFT_CLASS_NAMED("Sticker") @interface LINESticker : NSObject - (nonnull instancetype)init OBJC_DESIGNATED_INITIALIZER; @end Swift Objective-C SWIFT→OBJC→SWIFT
  58. 58. Swift Objective-C SWIFT→OBJC→SWIFT func presentStickerViewController() { let stickerViewController = StickerViewController() stickerViewController.sticker = Sticker() present(stickerViewController, animated: true, completion: nil) } // StickerViewController.h @class LINESticker; @interface StickerViewController : UIViewController @property (nonatomic, strong, nonnull) LINESticker *sticker; @end ?
  59. 59. SOLUTION ✓ DO NOT RENAME
 
 ✓ TYPEALIAS @objc class Sticker: NSObject { } typealias Sticker = LINESticker @objc class LINESticker: NSObject { }
  60. 60. SWIFT→OBJC ‣ forward declaration in ObjC header ‣ rewrite Protocols in ObjC when conforming ‣ - (id<LINEProduct>)theme;
 SWIFT→OBJC→SWIFT ‣ typealias RECAP
  61. 61. ANY QUESTIONS?
  62. 62. welcomepig welcomepig@gmail.com WE ARE HIRING
  63. 63. THANK YOU $$$

×