Swift: Type 'NotificationCenter' has no member 'default'

Viewed 42

I have an Observable Object class to get keyboard height and animation duration. I've been using this code in my previous project without any problems. The Xcode versions are the same and one of them does not give an error, while the other gives an error.

import Foundation
import SwiftUI
import Combine

class KeyboardController: ObservableObject {
    @Published var keyboardHeight: CGFloat = 0
    @Published var height: CGFloat = 0
    @Published var duration: CGFloat = 0
    
    init() {
        self.listenForKeyboardNotifications()
    }
    
    private func listenForKeyboardNotifications() {
        NotificationCenter.default.addObserver(forName: UIResponder.keyboardWillShowNotification,
                                               object: nil,
                                               queue: .main) { (notification) in
            guard
                let userInfo = notification.userInfo,
                let keyboardRect = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect,
                let duration = userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as? CGFloat
            else { return }
            
            self.duration = duration
            self.height = keyboardRect.height
            withAnimation(.spring(response: duration, dampingFraction: 1, blendDuration: 0)){
                self.keyboardHeight = keyboardRect.height
            }
        }
        
        
        NotificationCenter.default.addObserver(
            forName: UIResponder.keyboardWillHideNotification,
            object: nil,
            queue: .main
        ) { (notification) in
            guard
                let userInfo = notification.userInfo,
                let duration = userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as? CGFloat
            else { return }
            self.duration = duration
            withAnimation(.spring(response: duration, dampingFraction: 1, blendDuration: 0)){
                self.keyboardHeight = 0
            }
        }
    }
}

Errors: enter image description here

As I mentioned above, the same code works fine in a different project in the same Xcode version. I would be very happy if you could help me with what the error or the problem I missed here is.

EDIT: I can say that the problem is completely based on this project. It doesn't give an error in a new Xcode project.

0 Answers
Related