0
0
Swiftprogramming~20 mins

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

Choose your learning style9 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.