Group Group Group Group Group Group Group Group Group

Challenge: View Reuse | raywenderlich.com


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/15234721-your-second-ios-and-swiftui-app/lessons/7

Hey Jessy,

After doing this challenge I’ve ended up refactoring the Book view to a reusable component so it allows me to show a book for both the list view and the detailed view using a single component. This is what I came up with:

However, there’s an error in the code that tells me that the asPreview property is being used before initialization inside the initializer function itself and that is a little confusing. I’m sure that if I just remove the initializer function it should work, but I want to know if it’s possible to use default parameters in this case.

Thanks!

The error is incorrect; the compiler doesn’t know how to properly deal with you attempting to assign a UIImage? to what is actually a State<UIImage?>.

To do that, you need to choose one of these options:

  1. Change your line to this:
_image = .init(initialValue: image)
  1. Move the line to the end of the initializer. That will generate the equivalent of the following, for you, which you can then overwrite:
_image = .init(initialValue: nil)

As for why the second option works, it looks like a property wrapper-related compiler bug to me, and I recommend sending it over to http://bugs.swift.org.

1 Like

All right, no problem, here’s the ticket if you want to check it out: https://bugs.swift.org/browse/SR-13896

Initializing using _image did the trick! Thanks!

1 Like

Hey, I’m having a weird issue - as far as I can tell the detail view displays correctly, but the spacer I put in the VStack doesn’t seem to have an effect when I look at the detail in the live preview. Here’s what it looks like in the DetailView Preview:

And here’s the live preview, after navigating from the list view:

You can see it’s centered on the screen here, where the one above has it at the top, where it should be.

This is super super minor, but at 3:50 you say you changed the alignment to leading using “Command, Control, Click”, but I believe the shortcut is “Control, Option, Click”