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

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.

codelynx

May 17, 2017
Tweet

More Decks by codelynx

Other Decks in Programming

Transcript

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

    / NSView • UIViewController / NSViewController • NSColor / UIColor • NSImage / UIImage • etc…
  2. 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
  3. 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
  4. 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   ^
  5. No error J04 MFUWJFX97JFX GSBNF$(3FDU Y Z XJEUI IFJHIU 

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

     PWFSSJEFGVODESBX @SFDU$(3FDU \   9$PMPSSFETFU    9#F[JFS1BUI PWBM*OSFDU GJMM   ^ ^
  7. 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   ^
  8. 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   ^ ^
  9. 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 ^
  10. NS/UIView methods iOS macOS GVODBEE4VCWJFX ⭕ ⭕ GVODCSJOH4VCWJFX ⭕ ❌

    GVODTFOE4VCWJFX ⭕ ❌ GVODJOTFSU4VCWJFX ⭕ ❌ GVODFYDIBOHF4VCWJFX ⭕ ❌ GVODEJE"EE4VCWJFX ⭕ ⭕
  11. 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