Chapter 10 Tutorial project - Data transformation in view controller

The sample project suggests that we add the following data transformation in the view controller class:

    switch rating {
    case 0.0..<3.5:
      image = UIImage(named: "bad")!
    case 3.5..<4.0:
      image = UIImage(named: "meh")!
    case 4.0..<4.75:
      image = UIImage(named: "good")!
    case 4.75...5.0:
      image = UIImage(named: "great")!
    default:
      image = UIImage(named: "bad")!
    }

Wouldn’t it be better to move this logic to the view model?

@jstrawn any comment?

Hi @nservidio! There’s a post with a similar question to yours if you want to take a look: MVVM - Why are the businesses stored in the ViewController - #3 by jrg.developer

The short answer is yes, that data transformation could go into the view model and would make sense, but it’s also ok to have in the ViewController because the primary purpose of a viewmodel is to transform model information into values that can be displayed on a view and this code in particular is about images on the view.

1 Like