Everything is a nail…when

The thing about swift is that it makes you think in a particular way, so whenever you see code it makes you think about how it would be done in Swift.
So, the code in question looked like

class NewBlog {
    init() {
        writeFirstPost()
    }

    private func writeFirstPost() {
        println("Hello world!")
    }
}

This code is fine, however in the true spirit of Swift, I believe this code should look something like

class NewBlog {
    init() {
        writePost(text: "First Post, Hello World!")
    }

    func writePost(text: String) {
        print(text)
    }
}

The function that the class has should be able to add text to the blog. In this case that is writePost that takes text as a string and in the init posts the same by default. The only problem with this is that every time you want to use this class, you would end up with a standard First Post.
There can be other ways to circumvent this issue however, this is a better translation of the code above.

Views All Time
Views All Time
811
Views Today
Views Today
1
Posted in Uncategorized.