GTK4 binding for Swift

Hello to the Gnome community thanks for all you do! Just was wondering how hard it would be to be able to use GTK4 in Swift programming on linux. Is getting GTK bindings for Swift a major task? I’m just picking up programming so i’m fresh on this stuff.

Any help would be appreciated

Sounds to be a very bad idea.

When I ever should consider propritary Swift, then only on mac and iphone to use apples native GUI.

For Linux we have Rust and many more nice languages, and I think Mr Droege and friends provides good GTK4 support.

For the effort: I started from scratch with gobject-introspection for the Nim bindings and it took me 1600 hours til now. I would suggest you to hire someone from the Rust team, I would guess for some months of fulltime work for someone familiar with GTK4, Rust and Swift.

A few weeks to get something meaningfully complete to get started with using it for actual projects seems realistic for someone who knows the target language, GObject and writing bindings.

Rust is a very different language to Swift though, Swift is much less explicit and strict. It depends on what you’re looking for, what you want to use it for, what your preferences are, to decide on a language.

I don’t know what the status of GTK4 bindings for other languages is, that’s something @ebassi would know better, but I think apart from Vala, Rust, Nim, Python and GJS (in random order) would mean waiting for a while. AFAIU even those are not all ready yet for GTK4 but that should be just a matter of time. Other languages will likely take a bit longer, and for the above languages you would at least find a lot of people who actually know them inside the GNOME community.

The Swift ecosystem on non-Apple platforms is rather small so for starting programming this seems not like an ideal choice unless you first want to invent the wheel :slight_smile:

Similarly with Go you would have a hard time doing desktop development on Linux, and would have more luck with server / backend development.

As you say that you’re just starting with programming, and GTK4 development is something you want to do, better select one of the languages that is popular in the GNOME community (see list above although I have no idea about the popularity of Nim).

That way you will be able to get more help, find more examples and will likely also have higher-quality bindings to use.

Welcome @Shawn_Youngblood,
the answer is yes if you want to have a full binding. If you just need to begin playing with a window, some buttons and simple widgets, it’s of course easier. But you need to have a good understanding of the way to write GTK programs, and of the C language syntax, and how to bind C functions with Swift.

Have you looked at ?


That would depend on degree of idiomaticity you exprect. With exception for preprocessor macros and var-args functions, Swift allows you to use all of the C code - docs. You just need to configure your SPM package correctly. If you just want to create windows on Linux, you might try TokamakUI/Tokamak or other hand-written bindings.

If you expect safe interface without usage of Unsafe Pointers, without the need to allocate your own buffers etc, you would need to generate bindings.

On the GNOME side, this process is simplified by using GObject Introspection. This tool (amongst many things) is able to scan C header files and generate XML files .gir. Those files contain metadata (including documentation) of the C API. The language of .gir files is written in Relax NG Compact. The tool is called g-ir-scanner and you don’t need to run it yourself. Those .gir files could be installed as a part of -dev packages using package manager of your distribution. In my case (Ubuntu 20.04) the files are stored in /usr/share/gir-1.0/.

On the Swift side - you have to write your own generator. The only existing project I am aware of is rhx/gir2swift which is used to generate the rhx/SwiftGtk. The gir2swift is a compact yet complex piece of software - trying to understand what it does by reading it’s code might be intimidating. At the same time, a lot of things are yet to be implemented in the gir2swift.

Finally, there is a catch. As for now, there is no simple way of subclassing GObject classes from Swift. You might take a leap of faith and just pass Swift structs to C, but you have to be sure, that the C part of the structure (the offset 0 varibale with C structs) is using C layout and be sure, that Swift wont touch those places of the struct (by creating wide-enough buffer).

I am contributor to the rhx’s repositories. If you’re interested in learning how to use Gtk from Swift or help us develop the binding generation tool, feel free to join us using #41.


Well Apple open-sourced Swift, so it’s not to different from Rust, Kotlin etc. that are all built on LLVM. Not saying that the languages don’t have differences. But I think all three (Rust, Kotlin, Swift) will win out eventually. And especially Swift when it gets concurrency model and a few other things to allow it to go low-level. I just personally liked the ability to have C/C++ code directly in your Swift code and calling Python within it too.

I’m a noob so that seems handy to me for some reason :rofl:

Right on, I’ll check that out and get on the discord there when i get a chance. Honestly if I could draw a pixel on the screen, I’d throw a party. I’ve always been more of a hardware guy but I just pick up Swift a month or so a go but then I decided to ditch my Mac and Windows stuff, because I’m just tired of the bloat, built-in spyware, and not having control of my machine.

I’m pretty fresh though so I would be about as much help as a bull in a china shop for the moment. But I do like the idea of contributing to the Gnome community as I don’t have plans on actually trying to get a job as a developer, just more as a hobby working on various projects. I just really liked Swift though.


This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.