Cannot find 'self' in scope; did you mean to use it in a type or extension context?

Cannot find 'self' in scope; did you mean to use it in a type or extension context? I was programing notifications for my app and this error came up can anyone help me?

import SwiftUI
import UserNotificationsUI
import UserNotifications

struct Notification: View {
    var body: some View {
        Text("Hello, World!")
    }
}

func chekForPermission() {
    let notificaionCenter = UNUserNotificationCenter.current()
    notificaionCenter.getNotificationSettings { settings in
        switch settings.authorizationStatus {
        case .authorized:
            self.dispatchNotification()
        case .denied:
            return
        case .notDetermined:
            notificaionCenter.requestAuthorization(options: [.alert, .sound]) { didAllow, error in
                if didAllow {
                    self.dispatchNotification()
                }
            }
            default:
                return
        }
    }
}

func dispatchNotification() {
    
}

struct Notification_Previews: PreviewProvider {
    static var previews: some View {
        Notification()
    }
}
Answered by Scott in 762921022

The error message is saying checkPermission is a global function rather than a method, and global functions have no concept of self. As the error message suggests, did you intend for those functions to be methods within your Notification type?

Accepted Answer

The error message is saying checkPermission is a global function rather than a method, and global functions have no concept of self. As the error message suggests, did you intend for those functions to be methods within your Notification type?

How can I change checkPermission from a global function to a method?

Cannot find 'self' in scope; did you mean to use it in a type or extension context?
 
 
Q