Model-View-View Model in Swift

Srdan Rasic wrote a good post on creating wrappers around value objects (like Strings, say) and custom-glue them to an application’s interface. It’s a technique I have used in my book on Mac architecture at one point but which seems to be tremendously useful for virtually anything view-related once you get to a high enough level of abstraction.

It’s weird that articles related to programming are so hard to quote. Instead of a quotation, let the resulting code illustrates what he has to say:

class ArticleViewController {
  var bodyTextView: UITextView
  var titleLabel: UILabel
  var dateLabel: UILabel
  var thumbnailImageView: UIImageView
   
  var viewModel: ArticleViewViewModel {
    didSet {
      viewModel.title.bindAndFire {
        [unowned self] in
        self.titleLabel.text = $0
      }
   
      viewModel.body.bindAndFire {
        [unowned self] in
        self.bodyTextView.text = $0
      }
   
      viewModel.date.bindAndFire {
        [unowned self] in
        self.dateLabel.text = $0
      }
   
      viewModel.thumbnail.bindAndFire {
        [unowned self] in
        self.thumbnailImageView.image = $0
      }
    }
  }
}

So now when viewModel’s properties change, the view controller will update the corresponding interface component. Sweet.

Exploring Mac App Development Strategies – Patterns & Best Practices for Clean Software Architecture on the Mac. Don’t worry about big software design decisions anymore. Take on Mac app development with solid principles, guided by extensive examples and explanation. Buy the book now!

Making Good Use of Singletons in Refactoring the iOS App Calendar Paste

Like I promised last weekend, I am going to write about the process of cleaning up the already rotten source code of Calendar Paste. In order to break massive view controllers into manageable pieces and un-tangle everything, I have to make sure that I don’t break the current implementation. Calendar Paste didn’t have any automated tests in place. To change this fact is my first priority.

Continue reading …

DEVONthink and Zettelkasten

My dear friend Marko Wenzel has written an excellent review about DEVONthink and how to use it as a Zettelkasten note archive.

I have used DEVONthink myself a few years ago, but I think Marko’s expertise has far outpassed everything I ever knew about the app. I hope he’ll show us the various preview and link building scripts he created, too, because they make his workflow truly unique.

I’m About to Release a Little E-book on Domain-Driven Design and Mac Application Development

I’m adding a file monitoring feature to the Word Counter. This is a huge change to the application’s source code. To tackle this problem with style, I mulled things over for a while and considered my existing application’s design. I wanted to try out a few new things, so it was a no-brainer to start a sample project from scratch and fiddle with it.

Continue reading …