I need to make a class conform to a protocol in Swift in order to implement a delegate. How would I do so?
class YourClass: SuperClassIfAny, FirstProtocol, SecondProtocol {
}
Note, though, that some protocols require you to implement delegate methods. For instance, UITableViewDataSource
requires you to implement
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int
and
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!
If those are not implemented by the class conforming to the protocol, Xcode will give you a compile error (always check the protocol declaration, Cmd + Click will show you what methods you must implement).
Xcode 6 beta 7 changed the protocol slightly for UITableViewDataSource to match the following syntax on the two required implementations:
6b7 : UITableViewDataSource
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell!
compared to 6b6
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell
Of key difference is that UITableView, NSIndexPath and UITableViewCell are no longer ' Implicitly Unwrapped Optionals '
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.