WWDC24 SALE: Save 50% on all my Swift books and bundles! >>

SOLVED - Confusion with Project 14 - Introduction to MVVM

Forums > 100 Days of SwiftUI

That will of course break a lot of code, but the fixes are easy – just add viewModel in various places. So, locations becomes $viewModel.locations, and selectedPlace becomes viewModel.selectedPlace.

Except it's not easy. I added "viewModel" to all the instances of "locations" and "selectedPlace" but I'm getting errors on the Annotation block. Here's the relevant part of my code:

        MapReader { proxy in
            Map(initialPosition: startPosition) {
                ForEach($viewModel.locations) { location in
                    Annotation(location.name, coordinate: location.coordinate) {
                        Image(systemName: "star.circle")
                            .frame(width: 44, height: 44)
                            .onLongPressGesture {
                                viewModel.selectedPlace = location
            .onTapGesture { position in
                if let coordinate = proxy.convert(position, from: .local) {
                    let newLocation = Location(id: UUID(), name: "New location", description: "", latitude: coordinate.latitude, longitude: coordinate.longitude)
            .sheet(item: $viewModel.selectedPlace) { place in
                EditView(location: place) { newLocation in
                    if let index = viewModel.locations.firstIndex(of: place) {
                        viewModel.locations[index] = newLocation

The errors are:

Cannot assign to property: 'coordinate' is a get-only property

Cannot assign value of type 'Binding<[Location]>.Element' (aka 'Binding<Location>') to type 'Location'


Remove $ aka binding in this line



Well that's very strange. The article seems to be wrong. Thank you.


Save 50% in my WWDC sale.

SAVE 50% To celebrate WWDC24, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.