iOS Development

Xcode 14 “Publishing modifications from inside view updates will not be allowed, this can trigger undefined habits” – Donny Wals

Written by admin


Revealed on: September 7, 2022

Expensive reader, for those who’ve discovered this web page you are in all probability encountering the error from the publish title. Let me begin by saying this publish does not give you a fast repair. As a substitute, it serves to point out you the occasion the place I bumped into this problem in Xcode 14, and why I imagine this problem is a bug and never an precise problem. I’ve final examined this with Xcode 14.0’s Launch Candidate. I’ve filed suggestions with Apple, the suggestions quantity is FB11278036 in case you need to duplicate my problem.

A number of the SwiftUI code that I have been utilizing tremendous for a very long time now has lately began developing with this purple warning.

Initially I believed that there was an opportunity that I used to be, actually, doing one thing bizarre all alongside and I began chipping away at my undertaking till I had one thing that was sufficiently small to solely cowl a number of strains, however nonetheless advanced sufficient to characterize the actual world.

In my case, the problem occurred with the next code:

class SampleObject: ObservableObject {
    @Revealed var publishedProp = 1337

    func mutate() {
        publishedProp = Int.random(in: 0...50)
    }
}

struct CellView: View {
    @ObservedObject var dataSource: SampleObject

    var physique: some View {
        VStack {
            Button(motion: {
                dataSource.mutate()
            }, label: {
                Textual content("Replace property")
            })

            Textual content("(dataSource.publishedProp)")
        }
    }
}

struct ContentView: View {
    @StateObject var dataSource = SampleObject()

    var physique: some View {
        Checklist {
            CellView(dataSource: dataSource)
        }
    }
}

This code actually does nothing outrageous or bizarre. A faucet on a button will merely mutate an @Revealed property, and I count on the record to replace. Nothing fancy. Nonetheless, this code nonetheless throws up the purple warning. Compiling this identical undertaking in Xcode 13.4.1 works tremendous, and older Xcode 14 betas additionally do not complain.

At this level, it looks as if this may be a bug in Checklist particularly as a result of altering the record to a VStack or LazyVStack in a ScrollView doesn’t give me the identical warning. This tells me that there’s nothing basically mistaken with the setup above.

One other factor that appears to work round this warning is to alter the kind of button that triggers the motion. For instance, utilizing a bordered button as proven under additionally runs with out the warning:

Button(motion: {
    dataSource.mutate()
}, label: {
    Textual content("Replace property")
}).buttonStyle(.bordered)

Or if you’d like your button to appear like the default button fashion on iOS, you need to use borderless:

Button(motion: {
    dataSource.mutate()
}, label: {
    Textual content("Replace property")
}).buttonStyle(.borderless)

It form of appears to be like like something besides a default Button in a Checklist is ok.

For these causes, I sadly can’t offer you a correct repair for this problem. The issues I discussed are all workarounds IMO as a result of the unique code ought to work. All I can say is please file a suggestions ticket with Apple so we are able to hopefully get this fastened, documented, or in any other case defined. I will be requesting a code stage help ticket from Apple to see if an Apple engineer can assist me determine this out.

As soon as I’ve a greater image of precisely what’s taking place I’ll replace this publish.

About the author

admin

Leave a Comment