Avatar ·

Swift में NSNotificationCenter (addObserver) का सही उपयोग: सिंटैक्स और अनसब्सक्राइब समझें

नमस्ते! मैंने अभी Swift सीखना शुरू किया है और दो ViewController के बीच NSNotificationCenter के माध्यम से डेटा एक्सचेंज करने की कोशिश कर रहा हूँ। Objective-C में मैंने ऐसा सिंटैक्स देखा: ```objc [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:@"MyNotification" object:nil]; ``` लेकिन Swift में मैं उलझ गया। मैं ऐसे लिखने की कोशिश कर रहा हूँ: ```swift NSNotificationCenter.defaultCenter().addObserver(self, selector: "handleNotification:", name: "MyNotification", object: nil) ``` लेकिन Xcode selector पर त्रुटि देता है — कहता है कि #selector का उपयोग करें। मैंने फिर से लिखा: ```swift NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(handleNotification), name: "MyNotification", object: nil) ``` लेकिन अब मुझे समझ नहीं आ रहा कि handleNotification मेथड को सही तरीके से कैसे घोषित करूँ। क्या इसे Notification पैरामीटर लेना चाहिए? और मैंने सुना है कि नोटिफिकेशन से अनसब्सक्राइब करना ज़रूरी है, नहीं तो मेमोरी लीक होगी। Swift में यह सही तरीके से कैसे करें? कृपया दिखाएँ,