Slide 1

Slide 1 text

Using C in swift Kaz Yoshikawa

Slide 2

Slide 2 text

About me

Slide 3

Slide 3 text

Kaz Yoshikawa • Electricwoods LLC ୅ද / Digital Lynx Systems Inc. ෭୅ද • e-mail: [email protected] • 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.

Slide 4

Slide 4 text

Using C Library

Slide 5

Slide 5 text

Using C in Swift • SQLite • OpenSSL • zlib • libxml2 • mysqllib • libetpan • commonCrypto

Slide 6

Slide 6 text

Using C library in 
 your swift app case: 'sqlite3'

Slide 7

Slide 7 text

Create Bridging Header

Slide 8

Slide 8 text

Adding Any ObjC Classes

Slide 9

Slide 9 text

Create Bridging Header

Slide 10

Slide 10 text

Bridging Header

Slide 11

Slide 11 text

Don't forget adding 
 C libraries

Slide 12

Slide 12 text

The code…

Slide 13

Slide 13 text

Yah!! !

Slide 14

Slide 14 text

Using C Libraries in your swift Framework

Slide 15

Slide 15 text

There are some problems…

Slide 16

Slide 16 text

Bridging Header 
 cannot be used
 for Frameworks "

Slide 17

Slide 17 text

# Then what should I do?

Slide 18

Slide 18 text

Using Module Map $

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

Creating a folder

Slide 21

Slide 21 text

sqlite.h build for both simulator and device

Slide 22

Slide 22 text

Header Search Paths

Slide 23

Slide 23 text

Import Paths

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

Yes!! %

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

Yes!! &

Slide 28

Slide 28 text

module.modulemap

Slide 29

Slide 29 text

Clang

Slide 30

Slide 30 text

module.modulemap NPEVMFTRMJUF\ IFBEFSTRMJUFI FYQPSU ^

Slide 31

Slide 31 text

module-id NPEVMFTRMJUF\ IFBEFSTRMJUFI FYQPSU ^

Slide 32

Slide 32 text

Attributes NPEVMFTRMJUF\ 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.

Slide 33

Slide 33 text

header NPEVMFTRMJUF\ IFBEFSTRMJUFI FYQPSU ^

Slide 34

Slide 34 text

export NPEVMFTRMJUF\ 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.

Slide 35

Slide 35 text

module map issues

Slide 36

Slide 36 text

Can't open declaration cmd + click

Slide 37

Slide 37 text

github: UsingCinSwift

Slide 38

Slide 38 text

github: ZUnzip

Slide 39

Slide 39 text

github: SqliteKit

Slide 40

Slide 40 text

Yes!! &

Slide 41

Slide 41 text

C-callback

Slide 42

Slide 42 text

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

Slide 43

Slide 43 text

Oh well…. CGPath's C style callback

Slide 44

Slide 44 text

may the force is with you Stay tuned…..

Slide 45

Slide 45 text

Wrap Up

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

Thank you Kaz Yoshikawa
 [email protected]