Image Binding | 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/12

This Image Binding tutorial will not work with the current XCode 12.1 (even though the tutorial came out after this version of XCode).

The class Library needs to become an ObservableObject and uiImages needs to be marked with @Published:

final class Library: ObservableObject {

ā€¦

@Published var uiImages: [Book: UIImage] = [:]
}

Then the library variable in ContentView in ContentView.swift needs to be changed from @State to @StateObject

struct ContentView: View {
@StateObject var library = Library()
ā€¦

Your list in ContentView and DetailView will then update with the image that has been selected.

I just tested the uploaded materials again, with no problems. What youā€™ve shown here doesnā€™t solve any problem that exists at this point in the course. Please upload your version of the project without using a model object, and Iā€™ll have a look!

(My guess is that your Library is a class, instead of a struct.)

You are correct that I was using a class-not a struct-which was causing the problem. If you skip ahead to your supplied Starter and Final projects for 12-image-binding your version of Library uses a struct and the project works fine.

Unfortunately, I have worked through your projects in order and have carefully built my project from scratch with the exception of Library.swift which we added in 05-lists: in both the Starter and Final projects there, you have supplied a final class rather than a struct. Furthermore, your video actually shows Library.swift with a final class rather than a struct:
[Uploading: Library.swift screenshot.png.pngā€¦]
Hence the problemā€¦

2 Likes

Thanks for letting me know. Iā€™ve updated the downloadable materials and added an Author Note for Episode 5.

I went through this tutorial a week or so ago, and was unable to get images from the picker to show up on the detail view or list view.

I went through it AGAIN tonight (from scratch) to make sure i hadnā€™t mistyped anything and Iā€™m having the exact same issue.

Iā€™m on xcode 12.2. I copy/pasted the library file contents into my project but otherwise started from scratch.

Hi!

Please upload your project and Iā€™ll have a look.

And the way I will make a comparison to the downloadable final project is by using Git. Highly recommended learning!

Hmm, itā€™s saying new user cannot upload attachments

Iā€™d recommend pushing it to GitHub, or using Dropbox.

You are also using a class instead of a struct, as described earlier in this thread.

Thanks, same problem for me )

Hello,

Iā€™m trying to follow this course but in my simulator the Image arenā€™t saved.

this is the response of the console

2021-03-28 21:51:49.411365+0200 LeggiMi2021[12526:4401792] [LayoutConstraints] Unable to simultaneously satisfy constraints.

Probably at least one of the constraints in the following list is one you donā€™t want.

Try this:

(1) look at each constraint and try to figure out which you donā€™t expect;

(2) find the code that added the unwanted constraint or constraints and fix it.

(

ā€œ<NSLayoutConstraint:0x600001664280 ā€˜BIB_Trailing_CB_Leadingā€™ H:[_UIModernBarButton:0x14ce1e080]-(6)-[_UIModernBarButton:0x14ce1caf0ā€™La Mia Libreriaā€™] (active)>ā€,

ā€œ<NSLayoutConstraint:0x6000016642d0 ā€˜CB_Trailing_Trailingā€™ _UIModernBarButton:0x14ce1caf0ā€™La Mia Libreriaā€™.trailing <= _UIButtonBarButton:0x14ce1bc70.trailing (active)>ā€,

ā€œ<NSLayoutConstraint:0x600001665040 ā€˜UINav_static_button_horiz_positionā€™ _UIModernBarButton:0x14ce1e080.leading == UILayoutGuide:0x600000c55340ā€™UIViewLayoutMarginsGuideā€™.leading (active)>ā€,

ā€œ<NSLayoutConstraint:0x600001665090 ā€˜UINavItemContentGuide-leadingā€™ H:[_UIButtonBarButton:0x14ce1bc70]-(0)-[UILayoutGuide:0x600000c55260ā€™UINavigationBarItemContentLayoutGuideā€™] (active)>ā€,

ā€œ<NSLayoutConstraint:0x60000164f4d0 ā€˜UINavItemContentGuide-trailingā€™ UILayoutGuide:0x600000c55260ā€™UINavigationBarItemContentLayoutGuideā€™.trailing == _UINavigationBarContentView:0x14cf0f8f0.trailing (active)>ā€,

ā€œ<NSLayoutConstraint:0x600001665810 ā€˜UIView-Encapsulated-Layout-Widthā€™ _UINavigationBarContentView:0x14cf0f8f0.width == 0 (active)>ā€,

ā€œ<NSLayoutConstraint:0x60000164f890 ā€˜UIView-leftMargin-guide-constraintā€™ H:|-(0)-UILayoutGuide:0x600000c55340ā€™UIViewLayoutMarginsGuideā€™ (active, names: ā€˜|ā€™:_UINavigationBarContentView:0x14cf0f8f0 )>ā€

)

Will attempt to recover by breaking constraint

<NSLayoutConstraint:0x600001664280 ā€˜BIB_Trailing_CB_Leadingā€™ H:[_UIModernBarButton:0x14ce1e080]-(6)-[_UIModernBarButton:0x14ce1caf0ā€™La Mia Libreriaā€™] (active)>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.

The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.