Hybrid programming in Apple system development (2): mutual calls between Swift and C, system development swift

Source: Internet
Author: User

Hybrid programming in Apple system development (2): mutual calls between Swift and C, system development swift
When performing mutual calls between Swift and C, it is necessary to first understand the type conversion relationship between the two languages:

C type Swift type
Bool CBool
Char, signed char CChar
Unsigned char CUnsignedChar
Short CShort
Unsigned short CUnsignedShort
Int CInt
Unsigned int CUnsignedInt
Long CLong
Unsigned long CUnsignedLong
Long CLongLong
Unsigned long CUnsignedLongLong
Wchar_t CWideChar
Char16_t CChar16
Char32_t CChar32
Float CFloat
Double CDouble
Next, I will demonstrate how to call the C method in Swift and create a Swift project: Create a C code file in the project: at this time, you will be prompted whether to generate the Bridging Header and choose create.

 

The code structure after the file is created is as follows, you can see that there is a file named: <Project Name>-Bridging-Header.h, which is the file bridging between Swift and C.

 

In the configuration of the project, we can see that the bridge file is specified here, so if you forget to create this file, you don't have to worry about it. Just create a new file and specify it here.

 

Code for implementing C: MyCFile. h

 

MyCFile. c

 

Introducing the C code header file in the bridge file is very simple. Just import it directly.

 

Then, you can directly call the C method in the swift file:

 

Next we will add some code to implement two points: 1. Call Swift code in C code 2. Data Transmission (taking string as an example) MyCFile. h

C's call to the Swift method is actually equivalent to registering a global function pointer. For more information, see the definition of SwiftCallbackFun.

There are some OC Syntax: ^ operator, which indicates that this is a closure, that is, Block. Functions in Swift are transmitted in the form of closures. _ Nonnull indicates that the object should not be null, because during mixed compilation, the Swift compiler does not know whether an Objective-C object is optional or non-optional, therefore, Xcode 6.3 introduces a new Objective-C feature: 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 should pay attention to two points: 1. The method for passing in string processing in C in Swift 2. assign a value to the global Callback first, in this way, the method from C to Swift can be called.

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.