Swift: Deconstruct SPF Getting Started

Learning Swift by trying to make a simple project

In my previous series “Learning Rust”. I decided to attempt to learn some fairly basic rust concepts and constructs by working on a fairly simple project. This seemed to go fairly well. So I am now going to attempt to work on the same project, but this time, using Swift.

This will be a package based project. There will be no UI being developed.

Setting up the Development Environment

Creating the Project Directory Structure

For this I did some searching and decided to follow this guide using the Xcode approach (Option 2).
I created a directory called SPFLibrary and created the package with the name DeconSpf

What Xcode Creates

.
└── DeconSpf
    ├── Package.swift
    ├── README.md
    ├── Sources
    │   └── DeconSpf
    │       └── DeconSpf.swift
    └── Tests
        ├── DeconSpfTests
        │   ├── DeconSpfTests.swift
        │   └── XCTestManifests.swift
        └── LinuxMain.swift
  • Sources/DeconfSpf/ is where the code for this project will reside.
  • Tests is where our tests will reside obviously.
  • A point to note here is, based on my experience so far, we can not directly call our code within the files located under Sources/DeconSpf. Xcode will complain. You actually need to write the code as tests under Test.
  • Xcode defaults to using main for the branch name.

This all creates a working package with a test that also passes.

DeconSpf.swift

struct DeconSpf {
    var text = "Hello, World!"
}

DeconSPfTests.swift

import XCTest
@testable import DeconSpf

final class DeconSpfTests: XCTestCase {
    func testExample() {
        // This is an example of a functional test case.
        // Use XCTAssert and related functions to verify your tests produce the correct
        // results.
        XCTAssertEqual(DeconSpf().text, "Hello, World!")
    }

    static var allTests = [
        ("testExample", testExample),
    ]
}

Running an initial Test

swift test

This produces the following output in the terminal, you can of course trigger the test in Xcode and get nice green icons.

[6/6] Linking DeconSpfPackageTests
Test Suite 'All tests' started at 2021-05-12 23:29:39.523
Test Suite 'DeconSpfPackageTests.xctest' started at 2021-05-12 23:29:39.524
Test Suite 'DeconSpfTests' started at 2021-05-12 23:29:39.524
Test Case '-[DeconSpfTests.DeconSpfTests testExample]' started.
Test Case '-[DeconSpfTests.DeconSpfTests testExample]' passed (0.088 seconds).
Test Suite 'DeconSpfTests' passed at 2021-05-12 23:29:39.612.
	 Executed 1 test, with 0 failures (0 unexpected) in 0.088 (0.088) seconds
Test Suite 'DeconSpfPackageTests.xctest' passed at 2021-05-12 23:29:39.612.
	 Executed 1 test, with 0 failures (0 unexpected) in 0.088 (0.088) seconds
Test Suite 'All tests' passed at 2021-05-12 23:29:39.612.
	 Executed 1 test, with 0 failures (0 unexpected) in 0.088 (0.089) seconds

Let’s stop there for today. I will take up the next steps soon.


See also