Hybrid programming in Apple System development (2): Mutual invocation of Swift and C

Source: Internet
Author: User

When making calls between Swift and C, it is necessary to first understand the type conversion relationship between the two languages:
C type Swift type
bool CBo OL
Char, signed char cchar
unsigned char CUNSIGNEDCH Ar
short CShort
unsigned short cunsignedshort
int CInt
unsigned int cunsignedint
long clong
unsigned long cunsignedlong
Long long clonglong
unsigned long long cunsignedlonglong
wchar_t Cwidechar
char16_t CChar16
char32_t CCHA R32
float cfloat
double cdouble
Let's start by demonstrating how to invoke the C method in swift and create a swift project: Create a C code file in the project: This time you will be prompted to generate the bridging Header, select Create.

After the creation of the file, the code structure is as follows, you can see a file named:< project name >-bridging-header.h, this is the bridge between Swift and C file.

In the configuration of the project you can see that the bridge file is specified here, so if you forget to create this file, do not worry, you can specify a new one here.

Code to implement C: MyCFile.h

Mycfile.c

The introduction of the C code in the bridge file header file, very simple, directly import a bit can be.

You can then call the C method directly in the Swift file:

Here we add some code to implement the two point: A, C code in the SWIFT code call two, data transfer (in the case of strings) MyCFile.h

c The call to the Swift method is actually equivalent to registering a global function pointer, which can be found in the definition of swiftcallbackfun.

There are some OC syntax in this: the ^ operator, which indicates that this is a closure, that is, the functions in Block,swift are delivered in the form of closures. __nonnull indicates that the object should not be empty, because at the time of mixing, the Swift compiler does not know whether a Objective-c object is optional or non-optional, so the Xcode 6.3 Introduces a new feature of objective-c: nullability annotations, which can be defined as __nullable or __nonnull. MYCFILE.C here is a callback to the Swift method.

Main.swift

The code in Swift has two points to note: First, the way in which incoming strings in C are processed in swift. Assign a value to the global callback first, so that you can call the method from C to Swift.

Hybrid programming in Apple System development (2): Mutual invocation of Swift and C

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.