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

Using C in Swift

79874cedccd1cf5baa8cb264b5091ee6?s=47 codelynx
September 23, 2016

Using C in Swift

While programming in Swift, sometime it is necessary to deal with C libraries. This presentation will give you quick overview of how to use C library from your swift apps, and/or swift frameworks. [swift3]

79874cedccd1cf5baa8cb264b5091ee6?s=128

codelynx

September 23, 2016
Tweet

Transcript

  1. Using C in swift Kaz Yoshikawa

  2. About me

  3. Kaz Yoshikawa • Electricwoods LLC ୅ද / Digital Lynx Systems

    Inc. ෭୅ද • e-mail: kaz@digitallynx.com • twitter: @codelynx1 • Working History • Adobe Systems (Tokyo) • Lionbridge (Tokyo) • Quark (Tokyo / Denver) • Hummingbird Communications (Mt. View, USA) • Fact International (Vancouver, Canada) • Perle Systems (Toronto, Canada), etc.
  4. Using C Library

  5. Using C in Swift • SQLite • OpenSSL • zlib

    • libxml2 • mysqllib • libetpan • commonCrypto
  6. Using C library in 
 your swift app case: 'sqlite3'

  7. Create Bridging Header

  8. Adding Any ObjC Classes

  9. Create Bridging Header

  10. Bridging Header

  11. Don't forget adding 
 C libraries

  12. The code…

  13. Yah!! !

  14. Using C Libraries in your swift Framework

  15. There are some problems…

  16. Bridging Header 
 cannot be used
 for Frameworks "

  17. # Then what should I do?

  18. Using Module Map $

  19. What is module map? Module maps
 
 Module maps are

    specified as separate files (each named module.modulemap) alongside the headers they describe, which allows them to be added to existing software libraries without having to change the library headers themselves. http://clang.llvm.org/docs/Modules.html#module-maps
  20. Creating a folder

  21. sqlite.h build for both simulator and device

  22. Header Search Paths

  23. Import Paths

  24. Now you can use C identifiers in swift code JNQPSU6*,JU

    JNQPSUTRMJUF QVCMJDDMBTT5FTU4RMJUF@J04/40CKFDU\  QVCMJDDMBTTGVODUFTU \  MFUGJMFQBUI /45FNQPSBSZ%JSFDUPSZ BT/44USJOH BQQFOEJOH1BUI$PNQPOFOU UFTUTRMJUF   WBSEC0QBRVF1PJOUFS OJM  MFUTUBUVTTRMJUF@PQFO@W GJMFQBUI EC 42-*5&@01&/@3&"%83*5&]42-*5&@01&/@$3&"5& OJM   TXJUDITUBUVT\  DBTF42-*5&@0,   QSJOU TRMJUFPQFO    MFUTUBUVTTRMJUF@DMPTF EC    TXJUDITUBUVT\   DBTF42-*5&@0,    QSJOU TRMJUFDMPTFE    EFGBVMU    QSJOU TRMJUFFSSPS= TUBUVT     ^  EFGBVMU   QSJOU TRMJUFFSSPS= TUBUVT    ^  ^ ^ make sure it's public
  25. Yes!! %

  26. make sure
 Header file in Framework 48*'5@$-"44 @5U$UFTU@TRMJUF@JPT@GSBNFXPSL5FTU4RMJUF@J04  !JOUFSGBDF5FTU4RMJUF@J04/40CKFDU

     WPJE UFTU  OPOOVMMJOTUBODFUZQF JOJU0#+$@%&4*(/"5&%@*/*5*"-*;&3 !FOE
  27. Yes!! &

  28. module.modulemap

  29. Clang

  30. module.modulemap NPEVMFTRMJUF<TZTUFN>\ IFBEFSTRMJUFI FYQPSU  ^

  31. module-id NPEVMFTRMJUF<TZTUFN>\ IFBEFSTRMJUFI FYQPSU  ^

  32. Attributes NPEVMFTRMJUF<TZTUFN>\ IFBEFSTRMJUFI FYQPSU  ^ The system attribute specifies

    that the module is a system module. When a system module is rebuilt, all of the module’s headers will be considered system headers, which suppresses warnings. This is equivalent to placing #pragma GCC system_header in each of the module’s headers.
  33. header NPEVMFTRMJUF<TZTUFN>\ IFBEFSTRMJUFI FYQPSU  ^

  34. export NPEVMFTRMJUF<TZTUFN>\ IFBEFSTRMJUFI FYQPSU  ^ Export declaration¶ An export-declaration

    specifies which imported modules will automatically be re-exported as part of a given module’s API.
 
 The export-declaration names a module or a set of modules that will be re-exported to any translation unit that imports the enclosing module. Each imported module that matches the wildcard-module-id up to, but not including, the first * will be re-exported.
  35. module map issues

  36. Can't open declaration cmd + click

  37. github: UsingCinSwift

  38. github: ZUnzip

  39. github: SqliteKit

  40. Yes!! &

  41. C-callback

  42. C style callback • CGPathApplierFunction() • CGPatternCallbacks() • CGPDFOperatorTableSetCallback()

  43. Oh well…. CGPath's C style callback

  44. may the force is with you Stay tuned…..

  45. Wrap Up

  46. Wrap Up • There are so many good libraries out

    there! • Use Bridging Header, if you like to use C library in your app • Use module, if you like to use C library in your framework
  47. Thank you Kaz Yoshikawa
 kaz@digitallynx.com