Swift Protocol (Protocol) Associatedtype Association type

Source: Internet
Author: User
Tags vars

When defining a protocol, it is useful to declare one or more association types as part of the protocol definition. An association type provides a placeholder (or alias) for a type in the protocol, and the actual type it represents is not specified until the protocol is accepted. You can specify the association type by associatedtype the keyword. For example, the method of updating a cell with a protocol declaration:
  1. Model
  2. struct Model {
  3. Let age:int
  4. }
  5. protocols, using association types
  6. Protocol Tableviewcell {
  7. Associatedtype T
  8. Func Updatecell (_ data:t)
  9. }
  10. Compliance with Tableviewcell
  11. Class Mytableviewcell:uitableviewcell, Tableviewcell {
  12. Typealias T = Model
  13. Func Updatecell (_ data:model) {
  14. //do something ...
  15. }
  16. }

Swift Protocol (Protocol) Associatedtype Association type

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.