UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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.


TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

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.