Skip to content

Instantly share code, notes, and snippets.

@isthisjoe
Last active March 8, 2024 19:49
Show Gist options
  • Save isthisjoe/5ce421e290a723b11b81a0f21ed12417 to your computer and use it in GitHub Desktop.
Save isthisjoe/5ce421e290a723b11b81a0f21ed12417 to your computer and use it in GitHub Desktop.
Create Xcode Modular Architecture
  1. Open Xcode. File > New > Package
    1. Select Library > Next
    2. Choose the location that will be your root folder, enter the Package name > Create
    3. Rename the module created for you to something different than the Package and Product name.
    4. Under [Package name] > Sources > [Package Name] > [Package Name].swift, replace with this SwiftUI view for later use:
import SwiftUI

public struct TestLabel: View {
    public init() {}
    public var body: some View {
        Text("Testing")
    }
}
  1. File > New > Project
    1. Choose platform (e.g. iOS) > Select App > Next
    2. Enter product name > Change any project settings if needed > Next
    3. Open the Package root folder from Step 1 > Create
    4. Switch to the Package window and drag the Package into the Project window, positioning it directly under the Project (should be above the folder)
  2. Close both Package and Project window
  3. Create a new file under [Product Name]/Package.swift and paste below code. This prevents Xcode from re-importing the Source/Test code.
// swift-tools-version:5.2

// Leave blank. This is only here so that Xcode doesn't display it.

import PackageDescription

let package = Package(
  name: "client",
  products: [],
  targets: []
)
  1. Open [Product Name]/[Product Name].xcodeproj
  2. Rename the folder [Product Name] to the platform, e.g. iOS
  3. To test that the app can import the module:
    1. Under [Package Name]/Package.swift, add platforms: [.iOS(.v17)], just under the name attribute
    2. Open the Project Target > General > Under Framework, Libraries, and Embedded Content > Click Add > Search for [Package Name] and select the library with the test label
    3. Open ContentView.swift and add import [Package Name] and add in TestLabel() into the view
    4. Build and run app - you should see the test label in the view.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment