Black Friday deal: 30% off our developer tools, books and apps! Learn more ...Black Friday deal:30% off our tools, books and apps >>
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.

We are currently running a Black Friday promotion with 30% off SwiftFairy, our Swift and SwiftUI books and bundles, and Unlimited plans for our EPUB and PDF reader Whio. Visit the Black Friday page to learn more.

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

Black Friday 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
Black Friday 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