Avatar ·

كيفية الحصول على رابط إلى AppDelegate في Swift: UIApplication.shared.delegate والبدائل

مرحباً بالجميع! لقد بدأت للتو في تعلم Swift وتطوير تطبيقات iOS، وواجهت مشكلة. أحتاج إلى الوصول إلى بعض البيانات والطرق المخزنة في AppDelegate الخاص بي من وحدات التحكم والفئات الأخرى. أعلم أن AppDelegate هو singleton يتم إنشاؤه بواسطة النظام، لكنني لا أفهم كيفية الحصول على رابط إليه بالضبط.

إليك نسخة مبسطة من AppDelegate الخاص بي:

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    
    var window: UIWindow?
    var userToken: String? // رمز التوثيق المطلوب في العديد من وحدات التحكم
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        userToken = "some_token_123"
        return true
    }
    
    func someCustomMethod() {
        print("طريقة AppDelegate")
    }
}

الآن، في أحد ViewController's الخاصة بي، أحاول الوصول إلى