Group Group Group Group Group Group Group Group Group

Programming in Swift: Functions and Types · Challenge: Enumerations | raywenderlich.com


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/5429279-programming-in-swift-functions-and-types/lessons/22

For some reason my Swift Playground file doesn’t want to display images. Instead, it just shows me the value SwiftUI.AnyImageProviderBox. Is there an option somewhere to change that?

image

1 Like

Hi! Thanks so much for letting me know. It’s not just you. We lost support for displaying SwiftUI Views in Playgrounds since I recorded this course :crying_cat_face:

The good news is we can get it back by adding a little bit of code to the top of the Playground page. I’ll update the download materials to include this, but you can just paste this into the file you already have:

import UIKit
import SwiftUI

extension Image: CustomPlaygroundDisplayConvertible {
  public var playgroundDescription: Any {
    let hostingController = UIHostingController(
      rootView: self.resizable().padding()
    )
    let view: UIView = hostingController.view
    view.frame = CGRect(x: 0, y: 0, width: 200, height: 200)
    return view
  }
}

It’s not really important to understand that code right now! You’ll learn about extensions and protocols later in this course, so maybe come back to the explanation below after you finish it. Essentially here’s what this code is doing:

  • Saying you want SwiftUI’s Image type to conform to the CustomPlaygroundDisplayConvertible protocol in an extension. That protocol gives us a nice preview of Image in the Playground quick look! Right now, the only types that quick look works with are listed here: https://developer.apple.com/documentation/swift/customplaygrounddisplayconvertible
  • Adding the protocol’s required playgroundDescription property to Image.
  • Wrapping the Image in a UIKit UIView, giving it a size, and returning that UIView for the Playground to display.

I was wondering… why is it that instead of using += shorthand when adding the coins raw value to the total, you use just + ?

Hi! I’m using the version of reduce that returns a value from the closure. The result + coin.rawValue is short for

return result + coin.rawValue

The return keyword can be implicit in single line closures & function bodies.

I didn’t use += there for two reasons:

1 - result += coin.rawValue is short for:

result = result + coin.rawValue

I’d be trying to change the value of result, but result is a constant.

2 - Even if result was a variable, the += operation is only updating the value of result. I’d still need to return its value from the closure, like this:

  result += coin.rawValue
  return result

If I had used reduce(into:), I could have used += because result would be an inout parameter, and nothing is returned from the closure.

let total = coinPurse.reduce(into: 0.0) { (result, coin) in
  result += coin.rawValue
}

There’s no real reason I chose the other version of reduce for this problem. Both are valid solutions!

1 Like

Thank you very much that was very helpful!

1 Like

Hi there!! Any chance you can help me understand the result of my balance of the coinPurse here?

I have added a bunch of coins to the purse with a for loop, and then got the total. Shouldn’t the balance be a max of two decimal points though? What is happening here lol.