r/macosprogramming • u/rogerfin • Feb 13 '23
System programming - Swift or Objective C
Starting up with Mac OS system programming with quite a confusion. Should I pick Swift or Objective C or Objective C++? I don't need iphone/ipad/tv for now, just Mac OSX.
Read online that Swift is not compatible for older versions of Mac OS, frequently changing, so will require frequent software updates/releases. And am not sure if Swift is used for kext or SYSEX or other low level code, most libraries are in Objective C, I guess? Even if I learn Swift, will have to learn Objective C to call system level library functions?
Then if I go for Objective C and keep it simple, read that Apple is rewritting objective C libraries to Swift and may abandon Objective C in near future? For now, I have started Objective C, but not sure if doing right.
I mostly code in Rust, which has bindings for Core Foundation, Cocoa, etc. Will it be wise to try that or its a flawed approach for production?
Otherwise, would it be ok, without too much complexities and future issues, to have cross platform code written in Rust and export Mac OS specific code as FFIs to be called from Rust to keep a single Rust binary?
9
u/rcderik Feb 13 '23
I believe that, no matter what people claim the future will bring, you'll have to look at the current state of systems programming for macOS.
Most of the libraries, as you mention, are in Objective-C. Even if you believe that Swift will, at some point, overtake the percentage of Objective-C code, you'll still spend the next few years having to work with Objective-C.
You could go with Rust, but you'll swim against the current. It can be done, but you'll definitely have to come up with some "hacks" to make things work by yourself.
Systems programming for macOS is a dark art. There is very little updated documentation. You'll find yourself reading books like (To name a few):
- Advanced MAC OS X programming - Big nerd ranch - Objective-C
- MAC OS X Internals A Systems Approach - Amit Singh - C and Objective-C
- Apple's old documentation - Which is mostly in Objective-C
- *OS X Internals - Jonathan Levin - Mostly Objective-C
My day-to-day work is not as a systems programmer in macOS, but I've spent some time playing with it. Even when I tried to use Swift, which is what Apple "wants", I've had difficulty finding up-to-date documentation. I either have to read the source code or in some cases check FreeBSD Implementation.
For example:
https://rderik.com/blog/using-kernel-queues-kqueue-notifications-in-swift/ https://rderik.com/blog/using-bsd-sockets-in-swift/
So my 2¢ stay with Objective-C. Learn Swift and try to use it when possible but don't fight using Objective-C the task of programming in macOS is already hard enough without adding the complexity of interfacing with multiple languages simultaneously.
If you still want to go the Swift + Objective-C way, take a look at my articles, and see if you still want to go that route:
https://rderik.com/blog/making-a-c-library-available-in-swift-using-the-swift-package/ https://rderik.com/blog/understanding-objective-c-and-swift-interoperability/
I don't want to discourage you, but know it'll be a bumpy ride.
5
u/idelovski Feb 13 '23
Drivers used to be written in (simplified) C++ but maybe that changed recently.
By simplified I mean, basically C but with classes and not much else if I remember correctly. I have this book but never finished it or used it for something useful.
https://www.amazon.com/OS-X-iOS-Kernel-Programming/dp/1430235365
1
u/praveenperera Mar 04 '23
Why not just use Rust, since you’re already familiar with it?
2
u/pleaseinsertdisk2 Feb 15 '23 edited Feb 15 '23
The other two answers are giving a good overview and already point to some important documentation. I’ve got a few years of experience in systems programming for macOS and to a minor extent iOS and what I found looking into the most while actively developing are
Regarding the choice of language, I wouldn’t go for Rust as there is not a great community around it [ETA: for macOS, the Rust community itself is great!] and no native support on Apple’s side that I’m aware of. Objective-C is clearly on the way out of the door, so if you want to learn modern stuff don’t focus on that. But knowing it definitely helps, especially for understanding older code. C++, well, is C++. There will be a place for it in the future and it integrates easily with the low level C API.
The in my eyes most rewarding language choice for any Apple platform is Swift. It’s a great language to use with lots of evolutionary input from the C++ community. It’s really easy to do the right things and write safe code, contrary to my experience with C++. Integrating C API with Swift can be daunting, though. It generally is possible and makes calling into C very safe but expect to be confronted with a steep learning curve, especially regarding buffers and some suboptimal API ports. But other than that, Swift is easy to read and write and aims at being much less verbose than any of the other languages mentioned here. Having worked with all of these languages at some point in time, although little with Rust, I personally can wholeheartedly recommend Swift on macOS, even for system programming.
The only thing I’ve been missing over the years is a community of macOS system developers outside of my actual jobs. That’s still something I’d be interested in finding.