كيفية استخدام @selector() في Swift مع NSTimer والطرق الأخرى؟
مرحبًا بالجميع! أنا مبتدئ في Swift وأحاول فهم المحددات. لدي خبرة في Objective-C، حيث كنت أستخدم @selector() كثيرًا لتمرير الطرق إلى المؤقتات أو معالجات الأحداث. على سبيل المثال، أريد إنشاء مؤقت بسيط يستدعي طريقة كل ثانية. إليك الكود الخاص بي في Swift:
```swift
import UIKit
class ViewController: UIViewController {
var timer: Timer?
override func viewDidLoad() {
super.viewDidLoad()
// أحاول إنشاء مؤقت، لكني لا أعرف كيفية كتابة المحدد بشكل صحيح
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(updateTimer), userInfo: nil, repeats: true)
}
func updateTimer() {
print("تم تشغيل المؤقت!")
}
}
```
لكن Xcode يعطي خطأ: "Argument of '#selector' refers to instance method 'updateTimer()' that is not exposed to Objective-C". لا أفهم ما يعنيه هذا. في Objective-C، كنت أكتب @selector(updateTimer) وكان كل شيء يعمل. لماذا؟