ด้วยเหตุผลใดบ้างที่คุณจะใช้ส่วนขยายคลาสแยกต่างหากสำหรับตัวแทนแต่ละคนใน Swift
ฉันทำงานผ่านการสอนของ Ray Wenderlich และสังเกตว่าผู้เขียนใช้ส่วนขยายคลาสเพื่อระงับการโทรกลับแทนที่จะมอบหมายให้จัดการในคลาสเองเช่น: มอบหมาย callback ภายในส่วนขยายของชั้นเรียน: extension LogsViewController : UIPopoverPresentationControllerDelegate { func adaptivePresentationStyleForPresentationController(controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle { ... } } ตรงข้ามกับการมีอยู่ในชั้นเรียน: มอบหมายการเรียกกลับภายในชั้นเรียน: class LogsViewController : UITableViewController, UIPopoverPresentationControllerDelegate { func adaptivePresentationStyleForPresentationController(controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle { ... } } ฉันพบสิ่งนี้แปลกและน่าสนใจในเวลาเดียวกัน เขามีไฟล์เฉพาะสำหรับการขยายในคลาส LogsViewController ชื่อ "LogsViewControllerExtension.swift" และมีส่วนขยายที่แตกต่างกันสำหรับแต่ละโพรโทคอลของผู้แทน: UITableViewDataSource, UISplitViewDelegate …