Get started with Swift's C language library

Source: Internet
Author: User
Tags hmac

For a project on hand, I need to use the HMAC function in the Commoncrypto library. Although Apple has provided many system libraries in Swift, Commoncrypto is not in it. Fortunately, it's not much of a bother to use this library, just a little extra work.
Start accessing the Library
Before using the library, we need to notify the Swift compiler. To complete this process, we have two ways to do this. They all work in the sample project, but you should choose the exact way you want your code to be used. The good news is that you use that way, and then if you need another one in the back, To switch that is pretty straightforward.

Objective-c Bridge Header File

The simplest way to access a library is to use the OC Bridge header file. Because OC is a superset of C. So this bridge header file also allows you to access the pure C library. If your swift project does not have a bridge header file, create one yourself, and this step is simply going to die.
Right-click on your project and add an OC file, just a name (we'll delete the file later).

Once you have created this file, Xcode will ask you if you need to configure a OC Bridge header file, and of course choose Yes. Then delete the OC file you just created (not the header file ...). This header file will be named 项目名- Birdging-Header.h . Open this file and import the C library you need.

It is important to note that many libraries require additional linker settings, for example, in other linker identities that tell the -lfoo linker to link to Libfoo. Please set it in the BUiD settings of your Xcode project.

Module diagram

Although the way the bridge header file is so simple, it has a critical limitation-you can't framework use it in a type of project. So, to compensate for this flaw, you can use the "module"
To use "module", create a directory in your project directory (the directory name is your project name) and name the directory you want to use. I was on the command line, without Xcode's protection, and then named it conmmoncrypto. In this directory, create a Module.map file that contains some of the library's settings. That's what my commoncrypto,module.map looks like.

module CommonCrypto [system] {


header "/usr/include/CommonCrypto/CommonCrypto.h"

export *
}

Now add this new module to the Import paths-search Path under the Swift compiler entry in your project setting. S RC ROOT (Exampleas: {Srcroot}/commoncrypto) to ensure that the module works (relative path) no matter where you check out the project.
Now just make sure that you are able to import your module into the swift file. Note that all the frameworks you build yourself in this way must be imported into your project.

Using C functions

Once you have informed the Swift compiler about how you will reference the C library, you can use them directly in the SWIFT code. This process involves only figuring out which type of swift is matched by which type of C, and this process is very simple with Xcode's auto-completion, which can help us do most of the important things. This is an example of a SHA1 HMAC:










}

If you want to figure out how the call to Cchmac works, you can check here

This is the function prototype:
Cchmac(cchmacalgorithm algorithm, const void *key, size_t keylength,
Const void data, size_t datalength, void macout) ;

English Original address: http://spin.atomicobject.com/2015/02/23/c-libraries-swift/

Get started with Swift's C language library

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.