title: Developing a Swift iOS app "The Right Way"
body: <p>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.</p>
  
  <p>On my search, I've found this <a href="http://www.raywenderlich.com/86477/introducing-ios-design-patterns-in-swift-part-1">great tutorial</a> about all the design patterns normally used in a Swift iOS app and examples of where are they used.</p>
  
  <p>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:</p>
  
  <p>See the Façade Pattern implemented in LibraryAPI:</p>
  
  <pre><code>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() -&gt; [Album] {
          // ... Not relevant
      }
  
      func addAlbum(album: Album, index: Int) {
          // ... Not relevant
      }
  
      func deleteAlbum(index: Int) {
          // ... Not relevant
      }
  
      func downloadImage(notification: NSNotification) {
          // ... Not relevant
      }
  }
  </code></pre>
  
  <p>The first thing that comes to my mind seeing this is: Doesn't this violate the Depedency Inversion Principle? Shouldn't be <code>httpClient</code> and <code>persistencyManager</code> be declared as protocols and then the classes <code>HttpClient</code> and <code>PersistencyManager</code> implement that protocol?</p>
  
  <p>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?</p>
  
  <p>Another question I have is: This example only implements one Model (<code>Album</code>), but what if it would implement many others? (<code>Album</code>, <code>Author</code>, <code>Genre</code>...). Wouldn't be <code>LibraryAPI</code> be so big that it would violate the Single Responsability Principle?</p>
  
  <p>And last but not least... Same problem with the DIP exist in <code>PersistencyManager</code>. Shouldn't it implement DAO pattern, so `PersistencyManager does not depend on the other classes?</p>
  
  <p>Thank you in advance, and I hope I explained myself good enough!</p>