iTranslated by AI
A Conflict-Free Project Architecture Using .xcworkspace and Swift Packages Without Relying on XcodeGen
Summary
- In development using Xcode Projects (.xcodeproj), conflicts frequently occur due to
project.pbxproj. - Tools like XcodeGen have been used to prevent these conflicts, but I want to move toward a policy of not relying on external tools.
- By using an Xcode Workspace (.xcworkspace) and Swift Packages, we eliminate file operations within the Xcode Project (.xcodeproj) and prevent conflicts from occurring.
Preface
At CA.swift #14 〜Thinking about Next-Generation Architecture Now〜 held on May 19, 2021, @ry-itto(Ryoya Ito) gave a presentation titled "Insights gained from 3 weeks of development with the isowords configuration." The file structure introduced there was very interesting, so I decided to try it out for myself.
Overview
In application development using Xcode, developing with an Xcode Project (.xcodeproj) causes frequent conflicts in project.pbxproj. To prevent this, we have used tools such as XcodeGen.
However, when relying on external tools, you have to wait for those tools to be updated to support new Xcode features—for example, when a major annual Xcode update arrives and you want to add a new App Extension to a TARGET.
Furthermore, for cases like CI/CD, it is better to be able to operate using only xcodebuild.
Unlike an Xcode Project (.xcodeproj), where changes to project.pbxproj occur mainly through adding or deleting files, Swift Packages do not have a project.pbxproj or any equivalent file to manage the file structure. Therefore, you don't have to worry about this when adding or deleting files in a Swift Package.
Additionally, since Swift tools version 5.3, resource files can also be added to Swift Packages.
Since it is now possible to add Swift Packages to an Xcode Workspace (.xcworkspace) or an Xcode Project (.xcodeproj), combining these effectively can reduce conflicts by avoiding file operations within the Xcode Project (.xcodeproj).
Below is the configuration procedure for starting a SwiftUI iOS/macOS Multiplatform App project using an Xcode Workspace (.xcworkspace) and a Swift Package. The complete set of project files configured here is available on GitHub.
Configuration Procedure
-
Create an Xcode Workspace (.xcworkspace)
-
The state after creating the Xcode Workspace (.xcworkspace)
-
Create an Xcode Project (.xcodeproj) for the App
-
At this point, select the previously created Xcode Workspace (.xcworkspace) for "Add to" and "Group," then click "Create"
-
The state after creating MyApp.xcodeproj within the Xcode Workspace (.xcworkspace)
-
Create a Swift Package
-
At this point, select the previously created Xcode Workspace (.xcworkspace) for "Add to" and "Group," then click "Create"
-
The state after creating MyApp.xcodeproj and MyAppPackage within the Xcode Workspace (.xcworkspace)
-
Move
ContentView.swiftfrom MyApp to MyAppPackage
-
Change
internalparts topublicor similar to allow external access
-
Set the platforms for MyAppPackage (since we are using SwiftUI in
ContentView.swift, it's set to iOS 13.0 or later, etc.). Also, make it a dynamically linked library if necessary.
-
Add MyAppPackage to the MyApp TARGET
-
Import it with
import MyAppPackage
-
Build and Run to verify the behavior
From this point forward in development, you should basically implement everything within the Swift Package. If you want to use an external library, add it to the dependencies in the Package.swift of your Swift Package (e.g., MyAppPackage in the example above). If you must use a library that only supports CocoaPods or Carthage instead of Swift Package Manager, you can export those libraries as .xcframework and add them as a binaryTarget in the targets of your Package.swift.
Conclusion
I didn't know such a configuration was possible until @ry-itto(Ryoya Ito) presented it, and it's something I definitely want to use in the future.
Also, it was very interesting to realize that even for tools you might previously have used Mint for, you can simply add them to the Swift Package dependencies instead.
Other advantages and disadvantages not mentioned in this article were also discussed. The YouTube archive, SpeakerDeck, and the sample repository by @ry-itto(Ryoya Ito) are extremely helpful, so please check them out in the related items below.
Related Items
Discussion