SwiftUI - Infinite Loop - onDisappear without "."

Issue In Xcode version 15.1 iOS Version 16.6

Very strange, the following code create a never ending loop if I miss a single dot(.) in the syntax. You can see the "onAppear" message in the log. Somehow the compiler also not showing any error or warning. With my limited knowledge , I feel it's a kind of a bizard in any programming language. Think about the possible regression issues in complex projects due to this. And we will not get any build issues also. Please correct me if I am doing something wrong here. There are other similar issues like this, but I think I am able to reproduce this by removing a dot "."

Code with issue

.onAppear() {
    subscribeListeners()
    Logger.shared.debug("DownloadDataView: onAppear")
}
onDisappear() {
    unsubscribeListeners()
}

Correct code

.onAppear() {
    subscribeListeners()
    Logger.shared.debug("DownloadDataView: onAppear")
}
.onDisappear() {
    unsubscribeListeners()
}

Xcode version 15.1 iOS Version 16.6

Replies

with the dot, you're calling a function called onDisappear on the result of the function .onAppear.

without the dot, you're calling a function called onDisappear on the view enclosing the one you are applying modifiers to.

The latter is probably never what you want to do, but the compiler doesn't know that. All it knows is that the code compiles.

I think that the Swift compiler should take a look at what you write, and question the intent (even if the code compiles). There are rumors that Apple is doing big things with AI, maybe improved code review in Xcode could be one of those things? You can use Feedback Assistant to request such improvements.

Thank you @ssmith_c for the comment. I agree that it's not somebody will do intentionally. But the underneath logic in the compiler is wrong and triggers infinite loop on onAppear, that means the view is getting refreshed in a never ending loop. And that is something a serious bug in the underneath compilation logic. In this context if the onDisappear is not calling properly it's understandable but it should not trigger onAppear infinitely that's very serious issue.