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

    Simplified management of Amazon MSK with natural language using Kiro CLI and Amazon MSK MCP Server

    December 28, 2025

    Training a Model on Multiple GPUs with Data Parallelism

    December 28, 2025

    3D-Printed Cinema Film Camera Oozes Vintage Vibes

    December 28, 2025
    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

    ios – Background Assets Framework server connection problem

    December 27, 2025

    ios – SwiftUI-Button: Complete VStack clickable

    December 26, 2025

    ios – ld: framework ‘Pods_MyProjectName’ not found

    December 25, 2025
    Add A Comment
    Leave A Reply Cancel Reply

    Editors Picks

    Simplified management of Amazon MSK with natural language using Kiro CLI and Amazon MSK MCP Server

    December 28, 2025

    Training a Model on Multiple GPUs with Data Parallelism

    December 28, 2025

    3D-Printed Cinema Film Camera Oozes Vintage Vibes

    December 28, 2025

    Probing the fundamental nature of the Higgs Boson – Physics World

    December 28, 2025
    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!

    Simplified management of Amazon MSK with natural language using Kiro CLI and Amazon MSK MCP Server

    December 28, 2025

    Training a Model on Multiple GPUs with Data Parallelism

    December 28, 2025

    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
      © 2025 bigteetechhub.All Right Reserved

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