Adding a Swift Package dependency in Xcode
basic SPM workflow knowledge.
add via File menu, paste the repo URL, choose a version rule like up-to-next-major, link the product to your target.
What's really being asked
This is a baseline competency check: can you add a dependency the modern Apple-native way and reason about version pinning so builds stay reproducible.
The full answer
In Xcode you open the File menu and choose Add Package Dependencies, or select the project, the Package Dependencies tab, and the plus button. You paste the package's Git URL into the search field. Xcode fetches the package and presents a Dependency Rule: Up to Next Major Version is the common default, but you can choose Up to Next Minor, an Exact Version, a Version Range, a specific commit, or a branch. After resolving, Xcode asks which product or products from the package to add and to which target, which is what actually links the library. Xcode writes the resolved versions into Package.resolved so teammates and CI get identical versions.
The mistakes people make
Describing pod install and a Podfile, which is CocoaPods, not SPM. Forgetting the final step of attaching the product to a target, leaving the import unavailable. Always tracking a branch, which makes builds nondeterministic, when a semantic version range is the safer default.
What usually comes next
What does Package.resolved do and should you commit it? What is the difference between Up to Next Major and Exact? How do you update a package to a newer version later?
A concrete example
To add a networking library you choose File then Add Package Dependencies, paste its GitHub URL, select Up to Next Major Version starting at 5.0.0 so you accept 5.x but not a breaking 6.0, click Add Package, then tick its single library product and assign it to the app target. You commit the updated project file and Package.resolved so every build, local or CI, resolves the same revision until you deliberately update.
Interview question
After adding a package's URL in Xcode, what final action makes its API usable in your code?
- a.Switching the dependency rule to a branch
- b.Selecting the package product and assigning it to a build targetCorrect
- c.Running pod install from the terminal
- d.Deleting the Package.resolved file to force a re-resolve
Why? this is the answer
The package only links once you choose its product and attach it to a target; until then the import is unavailable. pod install is CocoaPods, deleting the lockfile harms reproducibility, and the branch rule does not affect linking.
Just read this? Test yourself on what you have been reading.
Read the original → developer.apple.com
- #ios
- #swift
- #swift-package-manager
- #dependencies
- #xcode
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on ios — each one lists the topics its interview covers.
See open roles