# Tutorial 3: My Locations. Solution to Exercise at p. 222

**URL:** <https://forums.kodeco.com/t/tutorial-3-my-locations-solution-to-exercise-at-p-222/36735>\
**Category:** iOS Apprentice\
**Created:** [November 20, 2017, 11:30pm UTC](https://forums.kodeco.com/t/tutorial-3-my-locations-solution-to-exercise-at-p-222/36735 "2017-11-20T23:30:19Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![anatoliy](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/anatoliy/32/73603_2.png) [@anatoliy](https://forums.kodeco.com/u/anatoliy)\
**Post date:** [November 20, 2017, 11:30pm UTC](https://forums.kodeco.com/t/tutorial-3-my-locations-solution-to-exercise-at-p-222/36735/1 "2017-11-20T23:30:19Z")

</div>

The exercise task at p. 222 to rewrite the logic to use a didSet property observer on the image instance variable so placing the photo into image will automatically update the UIImageView, without needing to call show(image) looks not so clear to me as you either need to call show(image) from the observer or move the logic from that function to the observer so I ended with this:

```
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

    image = info[UIImagePickerControllerEditedImage] as? UIImage

    tableView.reloadData()
    dismiss(animated: true, completion: nil)
}

var image: UIImage? {
    didSet {
        if let theImage = image {
            show(image: theImage)
        }
    }
}

```

Has anyone avoided that calling show(image)?

---

<div class="post-metadata">

**Author:** ![anon40008881](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@anon40008881](https://forums.kodeco.com/u/anon40008881)\
**Post date:** [December 15, 2017, 9:11am UTC](https://forums.kodeco.com/t/tutorial-3-my-locations-solution-to-exercise-at-p-222/36735/2 "2017-12-15T09:11:05Z")

</div>

Have the same question…

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [December 16, 2017, 2:19pm UTC](https://forums.kodeco.com/t/tutorial-3-my-locations-solution-to-exercise-at-p-222/36735/3 "2017-12-16T14:19:43Z")

</div>

@hollance @fahim Can you please help with this one when you get a chance? Thank you - much appreciated! :]

---

<div class="post-metadata">

**Author:** ![hollance](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/hollance/32/109009_2.png) [@hollance](https://forums.kodeco.com/u/hollance)\
**Post date:** [December 16, 2017, 2:42pm UTC](https://forums.kodeco.com/t/tutorial-3-my-locations-solution-to-exercise-at-p-222/36735/4 "2017-12-16T14:42:11Z")

</div>

You could try to move the code that is inside `show(image:)` into the `didSet`.

---

<div class="post-metadata">

**Author:** ![anatoliy](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/anatoliy/32/73603_2.png) [@anatoliy](https://forums.kodeco.com/u/anatoliy)\
**Post date:** [January 26, 2018, 2:52am UTC](https://forums.kodeco.com/t/tutorial-3-my-locations-solution-to-exercise-at-p-222/36735/5 "2018-01-26T02:52:13Z")

</div>

Moving logic into the didSet makes no sense from OO point of view because you lose encapsulation.

---

<div class="post-metadata">

**Author:** ![hollance](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/hollance/32/109009_2.png) [@hollance](https://forums.kodeco.com/u/hollance)\
**Post date:** [January 26, 2018, 10:07am UTC](https://forums.kodeco.com/t/tutorial-3-my-locations-solution-to-exercise-at-p-222/36735/6 "2018-01-26T10:07:13Z")

</div>

In what sense do you think we lose encapsulation?

---

<div class="post-metadata">

**Author:** ![anatoliy](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/anatoliy/32/73603_2.png) [@anatoliy](https://forums.kodeco.com/u/anatoliy)\
**Post date:** [January 26, 2018, 8:16pm UTC](https://forums.kodeco.com/t/tutorial-3-my-locations-solution-to-exercise-at-p-222/36735/7 "2018-01-26T20:16:51Z")

</div>

The logic of showing an image is encapsulated in this function. If in the future the requirements will change where or how to show an image you need to change the logic just in one place while all other places simply call this logic. Even if there are two places it’s hard to remember/recollecte where they are which gives you a potential bug.

---

<div class="post-metadata">

**Author:** ![hollance](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/hollance/32/109009_2.png) [@hollance](https://forums.kodeco.com/u/hollance)\
**Post date:** [January 27, 2018, 1:04pm UTC](https://forums.kodeco.com/t/tutorial-3-my-locations-solution-to-exercise-at-p-222/36735/8 "2018-01-27T13:04:46Z")

</div>

I don’t agree. By moving the logic for showing the image into the property for the image, you simplify the logic – now the only way to change the contents of the image view is through the `image` property. Therefore, it hides an implementation detail (the existence of the image view) and actually _increases_ encapsulation.

---

<div class="post-metadata">

**Author:** ![anatoliy](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/anatoliy/32/73603_2.png) [@anatoliy](https://forums.kodeco.com/u/anatoliy)\
**Post date:** [January 27, 2018, 1:35pm UTC](https://forums.kodeco.com/t/tutorial-3-my-locations-solution-to-exercise-at-p-222/36735/9 "2018-01-27T13:35:16Z")

</div>

In the current state of affairs, you’re right. The show(image:) is called in one place and moving in into the didSet is better. What I want to say that decoupling the logic from the didSet has an aim to ease development in the future if requirements change.

---

<div class="post-metadata">

**Author:** ![hollance](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/hollance/32/109009_2.png) [@hollance](https://forums.kodeco.com/u/hollance)\
**Post date:** [January 27, 2018, 1:52pm UTC](https://forums.kodeco.com/t/tutorial-3-my-locations-solution-to-exercise-at-p-222/36735/10 "2018-01-27T13:52:22Z")

</div>

Perhaps… it depends on the future. 😉 It’s usually impossible to see into the future, and many would argue that already preparing your code for a future that might never happen is not a good use of your time.

Of course, if you are 99% sure that you’re going to have to add a new feature to the app that requires you to have a separate `show(image:)` method, then it makes sense to have it as a separate method. But if not, then making it separate method “just in case” is not guaranteed to actually make your code any better.

---

<div class="post-metadata">

**Author:** ![anatoliy](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/anatoliy/32/73603_2.png) [@anatoliy](https://forums.kodeco.com/u/anatoliy)\
**Post date:** [January 27, 2018, 2:09pm UTC](https://forums.kodeco.com/t/tutorial-3-my-locations-solution-to-exercise-at-p-222/36735/11 "2018-01-27T14:09:51Z")

</div>

You’re quite right, my friend. This is the hardest part to find the ‘Golden Spot’ in decoupling and encapsulation. I bet, it’s a skill gained with an experience to predict what parts of your code are possible to be changed in the future.

---

<div class="post-metadata">

**Author:** ![system](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@system](https://forums.kodeco.com/u/system)\
**Post date:** [May 6, 2018, 3:30pm UTC](https://forums.kodeco.com/t/tutorial-3-my-locations-solution-to-exercise-at-p-222/36735/12 "2018-05-06T15:30:33Z")

</div>

Closed on May 6, 2018, 3:30pm UTC

This topic was automatically closed after 166 days. New replies are no longer allowed.
