traitCollectionDidChange

hello I am trying to work with this function but it does not work with IOS 17
I want to make a change every time I switched between dark and light mood

this is my function

override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
            super.traitCollectionDidChange(previousTraitCollection)
    
            // Check if the user interface style has changed
            if self.traitCollection.hasDifferentColorAppearance(comparedTo: previousTraitCollection) {
                // User interface style has changed (light to dark or vice versa)
                if self.traitCollection.userInterfaceStyle == .light {
                    // Code to execute in light mode
                    print("App switched to light mode")
                } else {
                    // Code to execute in dark mode
                    print("App switched to dark mode")
                }
            }
        }

and this is the issue
" ‘traitCollectionDidChange’ was deprecated in iOS 17.0: Use the trait change registration APIs declared in the UITraitChangeObservable protocol "