Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Developing a Swift iOS app "The Right Way"

Recently, I've learned Swift and the basics to develop an iOS app. Now, I want to develop a real application by my own, but I'm very concerned about writing good code, so I've looked up for "best practices", "design patterns" and "the right way" to achieve it.

On my search, I've found this great tutorial about all the design patterns normally used in a Swift iOS app and examples of where are they used.

But nevertheless I consider this tutorial a great one and helped me a lot, I have the feeling that it's only a start, because I see many S.O.L.I.D. principles violations. For example:

See the Façade Pattern implemented in LibraryAPI:

class LibraryAPI: NSObject {

    private let persistencyManager: PersistencyManager
    private let httpClient: HTTPClient
    private let isOnline: Bool

    class var sharedInstance: LibraryAPI {

        struct Singleton {
            static let instance = LibraryAPI()
        }

        return Singleton.instance
    }

    override init() {
        persistencyManager = PersistencyManager()
        httpClient = HTTPClient()
        isOnline = false

        super.init()
        NSNotificationCenter.defaultCenter().addObserver(self, selector:"downloadImage:", name: "BLDownloadImageNotification", object: nil)
    }

    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }

    func getAlbums() -> [Album] {
        // ... Not relevant
    }

    func addAlbum(album: Album, index: Int) {
        // ... Not relevant
    }

    func deleteAlbum(index: Int) {
        // ... Not relevant
    }

    func downloadImage(notification: NSNotification) {
        // ... Not relevant
    }
}

The first thing that comes to my mind seeing this is: Doesn't this violate the Depedency Inversion Principle? Shouldn't be httpClient and persistencyManager be declared as protocols and then the classes HttpClient and PersistencyManager implement that protocol?

If that's the case, at some point, I will have to define what classes, which implements those protocols, I'm going to use. Where should I tell the app?

Another question I have is: This example only implements one Model (Album), but what if it would implement many others? (Album, Author, Genre...). Wouldn't be LibraryAPI be so big that it would violate the Single Responsability Principle?

And last but not least... Same problem with the DIP exist in PersistencyManager. Shouldn't it implement DAO pattern, so `PersistencyManager does not depend on the other classes?

Thank you in advance, and I hope I explained myself good enough!

like image 786
barbarity Avatar asked May 21 '15 01:05

barbarity


People also ask

Do you need to know Swift to make iOS apps?

Swift is the coding language you'll need to learn for writing iOS apps. After downloading Xcode, you will be writing Swift into Xcode.

What percentage of iOS apps are written in Swift?

Results. According to this analysis, of the top 110 apps on the app store on January 15, 2019, 42% are using Swift, while 58% are not. If only the 79 non-game apps are considered, the results are that 57% are using Swift, while 43% are not.


1 Answers

A few suggestions

  1. Design patterns are a guide to help save you effort from resolving already solved problems, they aren't strict rules
  2. While the site you link to (raywenderlich.com) is a good start for tutorials, for a more detailed look at design patterns in swift I suggest Design Patterns In Swift
  3. If HttpClient and PersistencyManager are base classes which provide the interface than a protocol is not strictly essential. I agree that protocols are a more general way to go here
  4. If you go with protocols, I'd specify the client and persistency manager in the initializer as they are essential
  5. Persisting models is a specific enough role as to be handled by a single class, see realm.io for an example db
like image 106
Mark Essel Avatar answered Sep 29 '22 04:09

Mark Essel