SwiftUI Show init() and onAppear()?. 𠗘𠘅𠗽𠗹𠗼𠗿𠗲 𠗺𠘆 ð —šð —¶ð ˜ ð —›ð ˜‚ð —¯ ​​​​𠗳𠗼𠗿†¦ | by Shobhakar Tiwari | December 2024
1 min read

SwiftUI Show init() and onAppear()?. 𠗘𠘅𠗽𠗹𠗼𠗿𠗲 𠗺𠘆 ð —šð —¶ð ˜ ð —›ð ˜‚ð —¯ ​​​​𠗳𠗼𠗿†¦ | by Shobhakar Tiwari | December 2024


Stackademic

1 minute reading

December 11, 2024

𠗘𠘅𠗽𠗹𠗼𠗿𠗲 𠗺𠘆 ð —šð —¶ð ˜ ð —›ð ˜‚ð —¯ ​​​​𠗳𠗼𠗿 iOS Interview ð — ¿ð —²ð ˜€ð —¼ð ˜‚𠗿𠗰𠗲𠘀 : Shobhakar Github

During a Mentoring Session for Mock Interview on iOSA mentee asked me this, so I thought I’d share it with the developer community as well.

  • When? Called once when the view is created.
  • For what? Use it to configure properties or inject dependencies.

example:

struct WelcomeView: View {
let message: String

init(message: String) {
print("View Initialized")
self.message = message
}

var body: some View {
Text(message)
}
}

Use case: Configuration tasks that should only happen once when the view is first created.

  • When? Called every time the view appears on the screen.
  • For what? Use it for tasks like retrieving data, starting animations, or logging.

Example:

struct WelcomeView: View {
let message: String

var body: some View {
Text(message)
.onAppear {
print("View Appeared")
}
}
}

Use case: Perform actions when the view enters the visible area, even after being closed and redisplayed.

Key insight:

  • Initializer: Configuration tasks during creation (one time only).
  • onAppear(): Actions triggered every time the view appears.

Let me know what you think!

Before leaving:



Grpahic Designer