Challenge - 5 Problems
Swift Package Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ā Predict Output
intermediate2:00remaining
Understanding Swift Package Directory Structure
Given a Swift package with the following directory structure:
Which file is the main entry point for the library source code?
MyPackage/
āāā Package.swift
āāā Sources/
ā āāā MyLibrary/
ā āāā MyLibrary.swift
āāā Tests/
āāā MyLibraryTests/
āāā MyLibraryTests.swiftWhich file is the main entry point for the library source code?
Attempts:
2 left
š” Hint
Look inside the Sources folder for the main library code.
ā Incorrect
In Swift Package Manager, the main source code for a library is placed inside the Sources folder under a subfolder named after the library. Tests go under the Tests folder.
ā Predict Output
intermediate2:00remaining
Swift Package Manifest Basics
What is the output of running
swift package dump-package on this Package.swift file?import PackageDescription
let package = Package(
name: "MyPackage",
platforms: [.macOS(.v13)],
products: [
.library(name: "MyLibrary", targets: ["MyLibrary"])
],
targets: [
.target(name: "MyLibrary"),
.testTarget(name: "MyLibraryTests", dependencies: ["MyLibrary"])
]
)Attempts:
2 left
š” Hint
The command outputs the package manifest as JSON.
ā Incorrect
The
swift package dump-package command outputs the package manifest as a JSON object describing the package name, platforms, products, and targets.š§ Debug
advanced2:30remaining
Fixing a Build Error in Swift Package
You have this Package.swift snippet:
When running
Which option fixes the problem?
let package = Package(
name: "MyPackage",
products: [
.library(name: "MyLibrary", targets: ["MyLibrary"])
],
targets: [
.target(name: "MyLibrary"),
.testTarget(name: "MyLibraryTests", dependencies: ["MyLibrary"])
]
)When running
swift build, you get an error: error: no such module 'MyLibrary' in your test files.Which option fixes the problem?
Attempts:
2 left
š” Hint
The build system needs to know where the source files are.
ā Incorrect
If the source files are not in the default location, you must specify the path in the target definition. This fixes the module not found error.
š Syntax
advanced2:00remaining
Identify the Syntax Error in Package.swift
Which option contains a syntax error in the Package.swift manifest?
Attempts:
2 left
š” Hint
Look for missing commas between parameters.
ā Incorrect
Option A is missing a comma between the products and targets parameters, causing a syntax error.
š Application
expert2:30remaining
Determining Number of Targets in a Complex Package
Consider this Package.swift manifest:
How many targets (including test targets) does this package have?
let package = Package(
name: "ComplexPackage",
products: [
.library(name: "CoreLib", targets: ["Core"]),
.executable(name: "App", targets: ["App"])
],
targets: [
.target(name: "Core"),
.target(name: "Utils", dependencies: ["Core"]),
.target(name: "App", dependencies: ["Core", "Utils"]),
.testTarget(name: "CoreTests", dependencies: ["Core"]),
.testTarget(name: "AppTests", dependencies: ["App"])
]
)How many targets (including test targets) does this package have?
Attempts:
2 left
š” Hint
Count all .target and .testTarget entries.
ā Incorrect
There are 3 regular targets (Core, Utils, App) and 2 test targets (CoreTests, AppTests), totaling 5 targets.