How do I share code across iOS and macOS with SPM?

With Swift Package Manager (SPM), you can easily share code between your iOS and macOS applications. This allows you to maintain a single codebase and avoid code duplication, making your development process more efficient.

Here's how you can create a Swift package that can be used across both platforms:

// Step 1: Create a new Swift package // Open Terminal and run the following command swift package init --type library // Step 2: Add your shared code in Sources/YourPackageName/ // Example function that can be used in both iOS and macOS public func greet(name: String) -> String { return "Hello, \(name)!" } // Step 3: In your iOS and macOS projects, add the package as a dependency // Open Xcode, navigate to your project settings, and add the package // The URL will be the path to your package repository https://your-repo-url.git // Step 4: Import and use the package in your code import YourPackageName let greeting = greet(name: "World") print(greeting) // Outputs: Hello, World!

Swift Package Manager SPM iOS macOS code sharing Swift software development