WWDC 2026 deal: 30% off our Swift and SwiftUI books! Learn more ...WWDC 2026 deal:30% off our Swift and SwiftUI books >>
Quick Tip

Set supported platforms in file target membership options in Xcode

When we create a new file in Xcode, it's set to build for all platforms that the app supports by default. If the code in the file uses platform-specific APIs, such as NSDocumentController for macOS, the app won't compile for other platforms like iOS unless we wrap the platform-specific code in #if os(macOS) checks:

import SwiftUI

#if os(macOS)
struct MacDocumentList: View {
    
    var body: some View {
        List(
            NSDocumentController.shared.recentDocumentURLs,
            id: \.self
        ) { url in
            Text(url.absoluteString)
        }
    }
}
#endif

Alternatively, we can edit the file’s target membership options in Xcode and restrict the file to a specific platform. For instance, we can configure the file to only build for macOS. To do this, we open the file inspector panel in Xcode, select the target membership, and click the pencil icon to edit. From there, we uncheck the "Any Supported Platform" checkbox, choose the platform the file should build for, and click save.

Screenshot of Xcode shoing file target membership options

This avoids wrapping the entire file in conditional compilation checks when its contents are specific to one platform.

Note that this feature is only available for files managed by Xcode and it won't work in a Swift package.


If you're an experienced Swift developer looking to learn advanced techniques, check out my book Swift Gems. It dives into topics like optimizing collections, handling strings, and mastering asynchronous programming - perfect for taking your Swift skills to the next level.

I’m currently running a WWDC 2026 promotion with 30% off my books, plus additional savings when purchased as a bundle. Visit the books page to learn more.

Swift Gems by Natalia Panferova book coverSwift Gems by Natalia Panferova book cover

WWDC 2026 offer: 30% off!$35$25

100+ tips to take your Swift code to the next level

Swift Gemsby Natalia Panferova

  • Advanced Swift techniques for experienced developers bypassing basic tutorials
  • Curated, actionable tips ready for immediate integration into any Swift project
  • Strategies to improve code quality, structure, and performance across all platforms
WWDC 2026 offer: 30% off!

100+ tips to take your Swift code to the next level

Swift Gems by Natalia Panferova book coverSwift Gems by Natalia Panferova book cover

Swift Gems

by Natalia Panferova

$35$25