Pro Yearly is on sale from $80 to $50! »

iOS/macOS Cross Platform in Swift

iOS/macOS Cross Platform in Swift

Tips about how to share UI/NSView subclasses between macOS/iOS projects effectively.

79874cedccd1cf5baa8cb264b5091ee6?s=128

codelynx

May 17, 2017
Tweet

Transcript

  1. iOS / macOS Cross Platform Tips Kaz Yoshikawa Swift 2017

    May BUKURO x AKIBA Swift
  2. iOS / macOS 
 Cross Platform
 Programming in Swift

  3. Objective • Sharing code between iOS and macOS • UIView

    / NSView • UIViewController / NSViewController • NSColor / UIColor • NSImage / UIImage • etc…
  4. Type Aliasing JGPT J04  JNQPSU6*,JU UZQFBMJBT97JFX6*7JFX UZQFBMJBT9*NBHF6**NBHF UZQFBMJBT9$PMPS6*$PMPS UZQFBMJBT94DSPMM7JFX6*4DSPMM7JFX

    UZQFBMJBT97JFX$POUSPMMFS6*7JFX$POUSPMMFS FMTFJGPT NBD04  JNQPSU$PDPB UZQFBMJBT97JFX/47JFX UZQFBMJBT9*NBHF/4*NBHF UZQFBMJBT9$PMPS/4$PMPS UZQFBMJBT94DSPMM7JFX/44DSPMM7JFX UZQFBMJBT97JFX$POUSPMMFS/47JFX$POUSPMMFS FOEJG
  5. Some Differences J04 MFUWJFX97JFX GSBNF$(3FDU Y Z XJEUI IFJHIU 

    WJFXCBDLHSPVOE$PMPS9$PMPSCMVFOPFSSPS NBD04 MFUWJFX97JFX GSBNF$(3FDU Y Z XJEUI IFJHIU  WJFXCBDLHSPVOE$PMPS9$PMPSCMVF7BMVFPGUZQF97JFXIBTOPNFNCFS CBDLHSPVOE$PMPS
  6. Filling the Differences FYUFOTJPO97JFX\  JGPT NBD04   WBSCBDLHSPVOE$PMPS/4$PMPS

    \   HFU\    HVBSEMFUCBDLHSPVOE$PMPSTFMGMBZFS CBDLHSPVOE$PMPS
       FMTF\SFUVSOOJM^    SFUVSO/4$PMPS DH$PMPSCBDLHSPVOE$PMPS    ^   TFU\    TFMGXBOUT-BZFSUSVF    TFMGMBZFS CBDLHSPVOE$PMPSOFX7BMVF DH$PMPS   ^  ^  FOEJG   ^
  7. No error J04 MFUWJFX97JFX GSBNF$(3FDU Y Z XJEUI IFJHIU 

    WJFXCBDLHSPVOE$PMPS9$PMPSCMVFOPFSSPS NBD04 MFUWJFX97JFX GSBNF$(3FDU Y Z XJEUI IFJHIU  WJFXCBDLHSPVOE$PMPS9$PMPSCMVFOPFSSPS
  8. Subclassing X • Storyboard for both iOS / macOS DMBTT.Z7JFX97JFX\

     PWFSSJEFGVODESBX @SFDU$(3FDU \   9$PMPSSFETFU    9#F[JFS1BUI PWBM*OSFDU GJMM   ^ ^
  9. Use #if where appropriate DMBTT.Z7JFX$POUSPMMFS97JFX$POUSPMMFS\  JGPT J04  

    !*#0VUMFUXFBLWBSQJODI(FTUVSF6*1JODI(FTUVSF3FDPHOJ[FS  !*#0VUMFUXFBLWBSUBQ(FTUVSF6*5BQ(FTUVSF3FDPHOJ[FS  FMTFJGPT NBD04   !*#0VUMFUXFBLWBSNBHOJGZ(FTUVSF/4.BHOJGJDBUJPO(FTUVSF3FDPHOJ[FS  !*#0VUMFUXFBLWBSDMJDL(FTUVSF/4$MJDL(FTUVSF3FDPHOJ[FS  FOEJG   ^
  10. Wrap a function GVOD93FDU'JMM @SFDU$(3FDU \  JGPT J04 

     6*3FDU'JMM SFDU   FMTFJGPT NBD04   /43FDU'JMM SFDU   FOEJG ^ DMBTT.Z7JFX97JFX\  PWFSSJEFGVODESBX @SFDU$(3FDU \   9$PMPSSFETFU    93FDU'JMM SFDU   ^ ^
  11. Use typealias for Delegates JGPT J04  UZQFBMJBT94DSPMM7JFX%FMFHBUF6*4DSPMM7JFX%FMFHBUF FMTFJGPT NBD04

     QSPUPDPM94DSPMM7JFX%FMFHBUF\^ FOEJG DMBTT.Z7JFX$POUSPMMFS97JFX$POUSPMMFS 94DSPMM7JFX%FMFHBUF\  !*#0VUMFUXFBLWBSTDSPMM7JFX94DSPMM7JFX  JGPT J04   GVODTDSPMM7JFX%JE4DSPMM @TDSPMM7JFX6*4DSPMM7JFX \     ^  FOEJG ^
  12. Still so many differences

  13. NS/UIView methods iOS macOS GVODBEE4VCWJFX ⭕ ⭕ GVODCSJOH4VCWJFX ⭕ ❌

    GVODTFOE4VCWJFX ⭕ ❌ GVODJOTFSU4VCWJFX ⭕ ❌ GVODFYDIBOHF4VCWJFX ⭕ ❌ GVODEJE"EE4VCWJFX ⭕ ⭕
  14. NS/UIBezierPath iOS macOS GVODNPWF GVODNPWF GVODBEE-JOF func line() GVODBEE$VSWF func

    curve() GVODBEE2VBE$VSWF ❌ WBSDH1BUI$(1BUI ❌
  15. Rule of Thumb • Use typealias to bind NS/UI Classes

    • Use #if to write platform specific code • Wrap NS/UI function • Use typealias or fake protocol to trick compiler for delegates and other protocols • Share your bridging code
  16. gist: codelynx/ XPlatform.swift

  17. Kaz Yoshikawa May the Swift be with you….

  18. Thank you