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

Xcode 14 “Publishing changes from within view updates is not allowed, this will cause undefined behavior

Forums > SwiftUI

1      

@NigelGee - just today i asked about how i can share a link from inside of the app(was gettin the same error), i used below code, the suggestions by donny wals do not work , i had used VStack but keep getting the error, this is very prominent in cases we use links that go out of app .... so may be we need to wait for solution ...

VStack {
                        ShareLink(item: link) {
                            Label("", systemImage: "square.and.arrow.up")
                                .font(.custom("Arial", size: 50))
                        }
                    }

1      

I'm reviewing 100 Days of SwiftUI Day 70 - the BuckitList project and I'm geting this runtime warning along with the warning: "Modifying state during view update, this will cause undefined behavior.".

Atached are my ContentView.swift and Location.swift files from my BuckitList project. Indentical to the code in the lecture!!

As best I can determine, it's some sort of bug in the Map View amd the MapAnnotation custom annotation in Xcode 14. I'm running Version 14.1 (14B47b).

So I'm assuming that's the issue as pointed out by NigelGee above. Just wanted to put this out here in case others are runnng into this issue. The code runs just fine, just getting these stupid purple runtime warnings!!!

My ContentView

//
//  ContentView.swift
//  BucketList
//
//  Created by Michael Knych on 11/17/22.
//

import SwiftUI
import MapKit
import LocalAuthentication

struct ContentView: View {

  @State private var mapRegion = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 50, longitude: 0), span: MKCoordinateSpan(latitudeDelta: 25, longitudeDelta: 25))

  @State private var locations = [Location]()

    var body: some View {
      ZStack {
        Map(coordinateRegion: $mapRegion, annotationItems: locations) { location in
          MapAnnotation(coordinate: location.coordinate) {
            VStack {
              Image(systemName: "star.circle")
                .resizable()
                .foregroundColor(.red)
                .frame(width: 44, height: 44)
                .background(.white)
                .clipShape(Circle())
              Text(location.name)
            }
          }
        }
        .ignoresSafeArea()

        Circle()
          .fill(.blue)
          .opacity(0.3)
          .frame(width: 32, height: 32)

        VStack {
          Spacer()

          HStack {
            Spacer()

            Button {
              let newLocation = Location(id: UUID(), name: "New Location", description: "", latitude: mapRegion.center.latitude, longitude: mapRegion.center.longitude)
              locations.append(newLocation)
            } label: {
              Image(systemName: "plus")
            }
            .padding()
            .background(.black.opacity(0.75))
            .foregroundColor(.white)
            .font(.title)
            .clipShape(Circle())
            .padding(.trailing)
          }
        }
      }   
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

My Location.swift file:

//
//  Location.swift
//  BucketList
//
//  Created by Michael Knych on 11/17/22.
//

import Foundation
import MapKit

struct Location: Identifiable, Codable, Equatable {
  let id: UUID
  var name: String
  var description: String
  let latitude: Double
  let longitude: Double
  var coordinate: CLLocationCoordinate2D {
    CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
  }

  // an example for documentation
  static var example = Location(id: UUID(), name: "Buckinham Palace", description: "Where King Charles lives", latitude: 50.501, longitude: -0.141)

  // a custom == function for the struct to only equate the unigue id property
  static func ==(lhs: Location, rhs: Location) -> Bool {
    lhs.id == rhs.id
  }

}

1      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.