FREE TRIAL: Accelerate your app development career with Hacking with Swift+! >>

Swift for iOS to Swift for macOS

Forums > macOS

I am having a difficult time finding swift example code targeted for macOS development. It seems the iOS world is so prolific that the macOS stuff is getting pushed down in the search engines.

So, I thought I would ask here to see if there is some kind of cross-walk that would show what one needs to do to convert swift iOS app/code to be compatible with macOS? I try running the examples and I get errors telling me various classes are unknown, which I guess means they aren't being imported into the macOS app.

It appears Apple is trying to get to the point where one day it doesn't matter which platform you're targeting in Swift, the code will work on any of their platforms. However, until that happens it seems different libraries have to be imported and different classes loaded in order to get something that works for iOS to work in macOS. I'm just not versed enough in the Swift language to know what I need to do. Some kind of help document would be great to have. So far I've not found anything.

   

I forgot to mention that in my search I did find this tutorial for porting an iOS app to macOS:

https://www.raywenderlich.com/571-porting-your-ios-app-to-macos which then led me to this document by Apple:

https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/OSX_Technology_Overview/MigratingFromCocoaTouch/MigratingFromCocoaTouch.html

And maybe this is all that is available. Just thought I would ask to see if there was something else that may be less intense, or shortcuts/tips & tricks to the process.

   

Swift as a language is the same on both iOS and macOS - there's no porting that you need to do to move logic between the two. What is different between the platforms are the frameworks they support. I'm assuming your issue mostly has to do with migrating from UIKit to AppKit.

UIKit and AppKit are the frameworks you use to interact with the app's UI on the device you're running on. Since you're coming from iOS I assume you're familiar with UIKit - it's what gives you all the UI-prefixed classes like UIView, UIViewController, UICollectionViewCompositionalLayout and so on. These are components built by Apple engineers and designed specifically to work on Apple's iOS and iPadOS devices (or at least that used to be the case until catalyst rolled around, but that's a story for another day).

MacOS has a similar, but incompatible framework for UI creation, called AppKit. AppKit actually served as the base for UIKit, but as iOS has enjoyed much more popularity than macOS in the recent years UIKit APIs have been largely refined and improved to be more modern, pleasant to work with and "swifty", whilst AppKit's macOS APIs have remained largely unchanged. Take UICollectionViewCompositionalLayout for example. This was a huge addition at WWDC 2019 last year, and AppKit got it's own version, NSCollectionViewCompositionalLayout, which had largely the same feature set as the UIKit version. But this year at WWDC 2020 the UIKit team introduced UICollectionLayoutListConfiguration, a modern API designed to replace UITableViews, but AppKit didn't get any of this love.

The point I'm trying to make is that even though the two frameworks look very similar (many UI-prefixed classes have their NS---- counterparts in AppKit), their APIs are often very different and the way the two frameworks work foundationally also differs significantly, so moving an app from one to the other isn't as simple as replacing "UI" with "NS". Resources for learning AppKit development are also very sparse (and often very out-of-date), the best resource I've found is Paul's "Hacking with macOS", which is a great introduction to macOS programming and does a great job of explaining the principles behind macOS development.

Ultimately, if you're going to be starting to learn macOS development now, I'd recommend you get Paul's book to introduce you to the important platform features (copy-paste, drag and drop, doc-based-apps, etc), but I'd advise to dive into SwiftUI development. That is the framework that will eventually be mostly universal across the platforms, and it will be what Apple prefers moving forward.

Hope that helped at least a bit.

--Jakub

   

Thanks for the response. I'm new to Swift altogether, just not programming in general. I am working to port a program I wrote in Python to Swift for macOS. I want it to be a native mac App that I may eventually sell subscriptions for through the App store.

But yes, I do understand that iOS (and other devices) are the mainstream programming out there. I purchased Paul's excellent book Hacking with Swift for macOS. I learned a lot about the way Swift works. I'm just trying to find more up-to-date information, tutorials, examples, etc. So far I've been searching stackoverflow for most of my needs and have come across a few tutorials that seem to work, even though they are for a much older version of Swift.

Version lifecycle in the XCode/Swift world seems to be pretty short lived. I see my system just updated to Catalina version 6 today and XCode is now at 11.6, which I am assuming that would be Swift 5.3 or 5.4. I don't know how to tell which Swift version it is in XCode, if that is even a thing.

Anyway, I'm trudging along and finding nuggets of information in the scattered reaches of the web and every now and then something just happens to work when I'm plugging away at it. :)

   

Hacking with Swift is sponsored by Sentry

SPONSORED With Sentry’s error and performance monitoring for iOS you see mobile vitals that actually matter, can solve any latency issues quickly, and learn how each release is performing over time.

Get started

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.