🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of n0mn0m

n0mn0m's solution

to Hello World in the Swift Track

Published at Oct 29 2020 · 0 comments
Instructions
Test suite
Solution

The classical introductory exercise. Just say "Hello, World!".

"Hello, World!" is the traditional first program for beginning programming in a new language or environment.

The objectives are simple:

  • Write a function which takes a name, say Alice, and returns the string with the given name, in this case "Hello, Alice!". If no name is passed in, then the function returns the string "Hello, World!". The function should be named hello.
  • Run the test suite and make sure that it succeeds.
  • Submit your solution and check it at the website.

If everything goes well, you will be ready to fetch your first real exercise.

Hint

Check out the test cases and expected results in HelloWorldTests.swift. Here are some test examples.

  • Calling hello() returns "Hello, World!"
  • Calling hello("Alice") returns "Hello, Alice!"

Setup

Go through the project setup instructions for Xcode using Swift:

http://exercism.io/languages/swift
http://exercism.io/languages/swift/tests

Notably from the source directory:

swift test runs tests
swift package generate-xcodeproj creates an Xcode project

Source

This is an exercise to introduce users to using Exercism http://en.wikipedia.org/wiki/%22Hello,_world!%22_program

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

HelloWorldTests.swift

import XCTest
@testable import HelloWorld

class HelloWorldTests: XCTestCase {

    func testNoName() {
        let expected = "Hello, World!"
        XCTAssertEqual(HelloWorld.hello(), expected, "When given no name, we should greet the world!")
    }

    func testSampleName() {
        let expected = "Hello, Alice!"
        XCTAssertEqual(HelloWorld.hello("Alice"), expected, "When given 'Alice' we should greet Alice!")
    }

    func testOtherSampleName() {
        let expected = "Hello, Bob!"
        XCTAssertEqual(HelloWorld.hello("Bob"), expected, "When given 'Bob' we should greet Bob!")
    }

    func testNoStrangeName() {
        let expected = "Hello, !"
        XCTAssertEqual(HelloWorld.hello(""), expected, "When given an empty string, it is strange, but should have a space and punctuation")
    }

    static var allTests: [(String, (HelloWorldTests) -> () throws -> Void)] {
        return [
            ("testNoName", testNoName),
            ("testSampleName", testSampleName),
            ("testOtherSampleName", testOtherSampleName),
            ("testNoStrangeName", testNoStrangeName),
        ]
    }
}

LinuxMain.swift

import XCTest
@testable import HelloWorldTests

XCTMain([
    testCase(HelloWorldTests.allTests),
    ])
//Solution goes in Sources
func hello(_ name: String = "World") -> String {
    return "Hello, \(name)!"
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?