Bird
Raised Fist0
Swiftprogramming~20 mins

Project structure and Swift Package Manager basics - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
šŸŽ–ļø
Swift Package Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ā“ Predict Output
intermediate
2:00remaining
Understanding Swift Package Directory Structure
Given a Swift package with the following directory structure:
MyPackage/
ā”œā”€ā”€ Package.swift
ā”œā”€ā”€ Sources/
│   └── MyLibrary/
│       └── MyLibrary.swift
└── Tests/
    └── MyLibraryTests/
        └── MyLibraryTests.swift

Which file is the main entry point for the library source code?
APackage.swift
BSources/MyLibrary/MyLibrary.swift
CTests/MyLibraryTests/MyLibraryTests.swift
DSources/MyLibraryTests/MyLibrary.swift
Attempts:
2 left
šŸ’” Hint
Look inside the Sources folder for the main library code.
ā“ Predict Output
intermediate
2: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"])
    ]
)
AA runtime error about unsupported platform
BA syntax error message due to missing dependencies
CAn empty JSON object {}
DA JSON representation of the package with name, platforms, products, and targets
Attempts:
2 left
šŸ’” Hint
The command outputs the package manifest as JSON.
šŸ”§ Debug
advanced
2:30remaining
Fixing a Build Error in Swift Package
You have this Package.swift snippet:
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?
AAdd <code>dependencies: []</code> to the .target definition for MyLibrary
BRename the test target to .testTarget(name: "MyLibraryTests", dependencies: ["MyLibraryTests"])
CAdd <code>path: "Sources/MyLibrary"</code> to the .target definition
DChange the product type from .library to .executable
Attempts:
2 left
šŸ’” Hint
The build system needs to know where the source files are.
šŸ“ Syntax
advanced
2:00remaining
Identify the Syntax Error in Package.swift
Which option contains a syntax error in the Package.swift manifest?
Alet package = Package(name: "MyPackage", products: [.library(name: "MyLib", targets: ["MyLib"])] targets: [.target(name: "MyLib")])
Blet package = Package(name: "MyPackage", products: [.library(name: "MyLib", targets: ["MyLib"])], targets: [.target(name: "MyLib")])
Clet package = Package(name: "MyPackage", products: [.library(name: "MyLib", targets: ["MyLib"])], targets: [.target(name: "MyLib")], dependencies: [])
Dlet package = Package(name: "MyPackage", platforms: [.macOS(.v13)], products: [.library(name: "MyLib", targets: ["MyLib"])], targets: [.target(name: "MyLib")])
Attempts:
2 left
šŸ’” Hint
Look for missing commas between parameters.
šŸš€ Application
expert
2:30remaining
Determining Number of Targets in a Complex Package
Consider this Package.swift manifest:
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?
A5
B4
C3
D6
Attempts:
2 left
šŸ’” Hint
Count all .target and .testTarget entries.

Practice

(1/5)
1. What is the main purpose of the Package.swift file in a Swift package?
easy
A. It stores the compiled binary files of the package.
B. It contains the source code of the Swift package.
C. It defines the package's name, dependencies, targets, and products.
D. It is used to run the Swift package tests.

Solution

  1. Step 1: Understand the role of Package.swift

    This file is the manifest that describes the package's configuration, including its name, dependencies, targets, and products.
  2. Step 2: Differentiate from other files

    Source code files contain code, binaries are compiled outputs, and tests are run by commands, not defined in this file.
  3. Final Answer:

    It defines the package's name, dependencies, targets, and products. -> Option C
  4. Quick Check:

    Package manifest = Package.swift = It defines the package's name, dependencies, targets, and products. [OK]
Hint: Remember: Package.swift is the package's blueprint file. [OK]
Common Mistakes:
  • Confusing Package.swift with source code files
  • Thinking Package.swift stores compiled binaries
  • Assuming Package.swift runs tests directly
2. Which command do you use to build a Swift package from the terminal?
easy
A. swift build
B. swift package
C. swift run
D. swift compile

Solution

  1. Step 1: Recall Swift Package Manager commands

    The command swift build compiles the Swift package source code into an executable or library.
  2. Step 2: Differentiate from other commands

    swift run runs the built executable, swift package manages package operations, and swift compile is not a valid command.
  3. Final Answer:

    swift build -> Option A
  4. Quick Check:

    Build command = swift build = swift build [OK]
Hint: Build your package with 'swift build' command. [OK]
Common Mistakes:
  • Using 'swift run' to build instead of run
  • Typing 'swift compile' which is invalid
  • Confusing 'swift package' with build command
3. Given this directory structure of a Swift package:
MyPackage/
ā”œā”€ā”€ Package.swift
ā”œā”€ā”€ Sources/
│   └── MyLib/
│       └── MyLib.swift
└── Tests/
    └── MyLibTests/
        └── MyLibTests.swift
What will happen when you run swift test inside MyPackage?
medium
A. It compiles and runs the tests defined in MyLibTests.swift.
B. It builds the package but does not run any tests.
C. It deletes the Tests folder.
D. It runs the source code in MyLib.swift directly.

Solution

  1. Step 1: Understand the purpose of swift test

    This command compiles the test target and runs all tests found in the Tests directory.
  2. Step 2: Identify test files and behavior

    The file MyLibTests.swift contains test code that will be executed by swift test. It does not build only or delete folders.
  3. Final Answer:

    It compiles and runs the tests defined in MyLibTests.swift. -> Option A
  4. Quick Check:

    swift test runs tests = It compiles and runs the tests defined in MyLibTests.swift. [OK]
Hint: Run tests with 'swift test' to execute test files. [OK]
Common Mistakes:
  • Thinking 'swift test' only builds without running tests
  • Assuming it deletes files or folders
  • Confusing running tests with running source code
4. You wrote a Package.swift file but when running swift build, you get an error about missing targets. What is a likely cause?
medium
A. You forgot to create the Tests folder.
B. The targets array in Package.swift is empty or missing the source target.
C. You ran swift build outside the package directory.
D. The Swift compiler is not installed.

Solution

  1. Step 1: Analyze the error about missing targets

    Missing targets usually mean the Package.swift file does not list the source targets properly in its targets array.
  2. Step 2: Check other options

    While running outside the directory or missing compiler causes other errors, the specific missing target error points to the manifest file's targets section.
  3. Final Answer:

    The targets array in Package.swift is empty or missing the source target. -> Option B
  4. Quick Check:

    Missing targets error = fix targets array in Package.swift [OK]
Hint: Check targets array in Package.swift if build says missing targets. [OK]
Common Mistakes:
  • Ignoring the targets section in Package.swift
  • Assuming missing Tests folder causes target errors
  • Not verifying current directory before building
5. You want to create a Swift package that produces a library named Utils and depends on an external package Alamofire. Which Package.swift snippet correctly sets this up?
hard
A. let package = Package(name: "Utils", dependencies: [.package(url: "https://github.com/Alamofire/Alamofire.git")], targets: [.target(name: "Utils")], products: [.library(name: "Utils", targets: ["Utils"])])
B. let package = Package(name: "Utils", targets: [.target(name: "Utils")], products: [.executable(name: "Utils", targets: ["Utils"])])
C. let package = Package(name: "Utils", dependencies: [], targets: [.target(name: "Utils")], products: [.library(name: "Utils", targets: ["Utils"])])
D. let package = Package(name: "Utils", dependencies: [.package(url: "https://github.com/Alamofire/Alamofire.git", from: "5.0.0")], targets: [.target(name: "Utils", dependencies: ["Alamofire"])], products: [.library(name: "Utils", targets: ["Utils"])])

Solution

  1. Step 1: Identify correct dependency syntax

    let package = Package(name: "Utils", dependencies: [.package(url: "https://github.com/Alamofire/Alamofire.git", from: "5.0.0")], targets: [.target(name: "Utils", dependencies: ["Alamofire"])], products: [.library(name: "Utils", targets: ["Utils"])]) correctly specifies the dependency with URL and version using from: "5.0.0", which is required for versioning.
  2. Step 2: Check targets and products

    let package = Package(name: "Utils", dependencies: [.package(url: "https://github.com/Alamofire/Alamofire.git", from: "5.0.0")], targets: [.target(name: "Utils", dependencies: ["Alamofire"])], products: [.library(name: "Utils", targets: ["Utils"])]) includes the dependency in the target's dependencies array and defines a library product named "Utils" targeting the "Utils" target.
  3. Step 3: Compare other options

    let package = Package(name: "Utils", targets: [.target(name: "Utils")], products: [.executable(name: "Utils", targets: ["Utils"])]) lacks dependencies and wrongly defines an executable product. let package = Package(name: "Utils", dependencies: [], targets: [.target(name: "Utils")], products: [.library(name: "Utils", targets: ["Utils"])]) has no dependencies. let package = Package(name: "Utils", dependencies: [.package(url: "https://github.com/Alamofire/Alamofire.git")], targets: [.target(name: "Utils")], products: [.library(name: "Utils", targets: ["Utils"])]) misses the version specifier for the dependency, which is required.
  4. Final Answer:

    let package = Package(name: "Utils", dependencies: [.package(url: "https://github.com/Alamofire/Alamofire.git", from: "5.0.0")], targets: [.target(name: "Utils", dependencies: ["Alamofire"])], products: [.library(name: "Utils", targets: ["Utils"])]) -> Option D
  5. Quick Check:

    Dependency with version + target dependency + library product = let package = Package(name: "Utils", dependencies: [.package(url: "https://github.com/Alamofire/Alamofire.git", from: "5.0.0")], targets: [.target(name: "Utils", dependencies: ["Alamofire"])], products: [.library(name: "Utils", targets: ["Utils"])]) [OK]
Hint: Specify dependency URL with version and add it to target dependencies. [OK]
Common Mistakes:
  • Omitting version in dependency declaration
  • Not adding dependency to target's dependencies
  • Using executable product instead of library
  • Leaving dependencies empty when needed
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard