Hi there,

I’m Charles Nyisztor, and I welcome you to this new episode on Swift 3.
Today I am going to show you how to use C in Swift.
Now, let me first talk a bit about the importance of being able to consume C from Swift code.

As you may know, there are plenty of useful, highly popular C libraries out there: ZLib, OpenSSL, SQLite3 just to name a few. If Swift did not maintain the compatibility with C, we’d have to re-implement the functionality that is already provided by existing C libraries.

In this demo, I am going to show you how to use the C quicksort function in a Swift playground.

All right, let’s declare an array of integers. We are going to sort it using a custom Swift function, which relies on the C qsort function.

Next, I am going to implement the Swift quicksort function. The function accepts a single inout argument, which is used to provide the array to be sorted, and holds the sorted array after the function completes.

Now we invoke the C qsort function. At this point, it is imperative to have the Foundation framework imported in order to access the low-level C APIs and libraries.

The qsort has the following arguments: the first one called base is a pointer to the first element of the array we want to sort.
We provide the address of the array, which will point to its first element.
With the next argument, we provide the number of items in the array.

The third parameter has to provide the size of each element. Since we are using array of integers, the size should reflect the size of an int. In prior versions of Swift we would use the sizeof function; however, the sizeof function is not supported anymore in Swift 3. Instead, we have to use the MemoryLayout struct, which provides a unified interface to sizeof and related functions. For more details check out the related Swift evolution proposal page.

I provided the link in the video description.

The last parameter is the comparator function. It has two arguments: the left and the right operands, which are unsafe raw pointers.
These are the addresses of the elements that come from our array; they will be compared using the implementation I am going to provide in a moment. But first, we must access the real values behind the unsafe raw pointers supplied by the left and right operands.

NewYearsPromoTechie-10usd728x90

Swift 3 changed the way we access the values behind unsafe raw pointers. In Swift 2.3 we would simply wrap the operands in an UnsafePointer, and access the value via its ‘memory’ property.

This won’t work in Swift 3 anymore. Instead, we have to convert the unsafe raw pointer to an unsafe pointer using the assumingMemoryBount(to:) function. We are invoking it conditionally since the arguments are conditional.

After that, we can access the value behind the unsafe pointer via its ‘pointee’ property. Finally, we can access the values behind the unsafe raw pointers.
Implementing the algorithm to compare the operands is easy: if the left value is smaller than the right one, we have to return minus one; if the values are equal, we return zero, and if the right value is bigger then the left one, we return one.

We are done with the heavy lifting part, so let’s try out our freshly baked quickSort function.
Perfect! The array is sorted as expected.

Note that normally you wouldn’t implement a custom sort function – this is just for the sake of this demo; Swift provides easy ways to sort arrays.
Here we go… isn’t it nice?

Check out my video on Swift 3 array algorithms to find out more.

The demo project is available on Github. You can find the link in the description of this video.

Don’t forget to subscribe to my channel if you are interested in Swift 3 related videos. I plan to add new videos about interesting topics on a regular basis. Also, feel free to visit my website and check out my apps, my online courses, and other cool stuff.

Best of luck!
Learn to make a game!

Leave A Comment