Close Menu
  • Home
  • AI
  • Big Data
  • Cloud Computing
  • iOS Development
  • IoT
  • IT/ Cybersecurity
  • Tech
    • Nanotechnology
    • Green Technology
    • Apple
    • Software Development
    • Software Engineering

Subscribe to Updates

Get the latest technology news from Bigteetechhub about IT, Cybersecurity and Big Data.

    What's Hot

    Here Come the Women in Construction

    March 3, 2026

    Mobile Spyware: How Hackers Hijack Phones Through App Spyware

    March 3, 2026

    How reversibility becomes irreversible – Physics World

    March 3, 2026
    Facebook X (Twitter) Instagram
    Facebook X (Twitter) Instagram
    Big Tee Tech Hub
    • Home
    • AI
    • Big Data
    • Cloud Computing
    • iOS Development
    • IoT
    • IT/ Cybersecurity
    • Tech
      • Nanotechnology
      • Green Technology
      • Apple
      • Software Development
      • Software Engineering
    Big Tee Tech Hub
    Home»iOS Development»ios – Different behavior when initializing object within init() vs onAppear – Confused
    iOS Development

    ios – Different behavior when initializing object within init() vs onAppear – Confused

    big tee tech hubBy big tee tech hubDecember 6, 2025011 Min Read
    Share Facebook Twitter Pinterest Copy Link LinkedIn Tumblr Email Telegram WhatsApp
    Follow Us
    Google News Flipboard
    ios – Different behavior when initializing object within init() vs onAppear – Confused
    Share
    Facebook Twitter LinkedIn Pinterest Email Copy Link


    If have tinkered with Swift AVAudioPlayer.

    Currently I’m confused because when I initialize the object within onAppear, then everything works fine.

    When I use init() for initialization, then the object is nil afterwards.

    I used the debugger: It’s perfectly clear, that the code within init, has been executed before the object becomes used. Nevertheless it’s nil, respectively “” afterwards.

    What goes wrong here?

    Can someone explain the described findings?

    import AVFAudio
    
    struct ContentView: View {
        @State var player: AVAudioPlayer? = nil
        @State var isPlaying = false
        
        let url = Bundle.main.url(forResource: "demo01", withExtension: "m4a")
        // Results in 
        init() {
    //      if let url {
    //          do {
    //              player = try AVAudioPlayer(contentsOf: url)
    //          } catch {
    //              print(error.localizedDescription)
    //          }
    //      }
        }
        
        var body: some View {
            VStack(spacing: 25) {
                Button {
                    if let player {
                        if (isPlaying == true) {
                            player.pause()
                            isPlaying = false
                        } else {
                            player.prepareToPlay()
                            player.play()
                            isPlaying = true
                        }
                    }
                } label: {
                    Label(isPlaying ? "Pause" : "Play",
                          systemImage: isPlaying ? "stop.circle" : "play.circle")
                }
            }
            .onAppear() {
                if let url {
                    do {
                        player = try AVAudioPlayer(contentsOf: url) // 
                    } catch {
                        print(error.localizedDescription)
                    }
                }
            }
            .padding()
        }
    }
    



    Source link

    behavior Confused init initializing iOS object onAppear
    Follow on Google News Follow on Flipboard
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email Copy Link
    tonirufai
    big tee tech hub
    • Website

    Related Posts

    swift – SwiftUI IOS 26 – Menu button floats outside its container during selection animation

    March 3, 2026

    I wish I could be an OpenClaw Maintainer

    March 2, 2026

    performance – The optimal way for loading large video from iOS gallery + reopen it on next app launch

    March 1, 2026
    Add A Comment
    Leave A Reply Cancel Reply

    Editors Picks

    Here Come the Women in Construction

    March 3, 2026

    Mobile Spyware: How Hackers Hijack Phones Through App Spyware

    March 3, 2026

    How reversibility becomes irreversible – Physics World

    March 3, 2026

    Khamenei killing: Is it legal to target a head of state in war?

    March 3, 2026
    About Us
    About Us

    Welcome To big tee tech hub. Big tee tech hub is a Professional seo tools Platform. Here we will provide you only interesting content, which you will like very much. We’re dedicated to providing you the best of seo tools, with a focus on dependability and tools. We’re working to turn our passion for seo tools into a booming online website. We hope you enjoy our seo tools as much as we enjoy offering them to you.

    Don't Miss!

    Here Come the Women in Construction

    March 3, 2026

    Mobile Spyware: How Hackers Hijack Phones Through App Spyware

    March 3, 2026

    Subscribe to Updates

    Get the latest technology news from Bigteetechhub about IT, Cybersecurity and Big Data.

      • About Us
      • Contact Us
      • Disclaimer
      • Privacy Policy
      • Terms and Conditions
      © 2026 bigteetechhub.All Right Reserved

      Type above and press Enter to search. Press Esc to cancel.