Swift Typealias Associatedtype

Source: Internet
Author: User

Use Typealias as an alias for a common data type,

On the one hand it is easier to understand the use of this type by alias,

On the other hand, you can reduce the amount of code you develop daily.

Typealias Usage Examples:

//Network request common callback closureTypealias successwithmsg = ((_ msg:string)Void) Typealias failwitherror= (Error)Void)//connect multiple protocols with &Typealias Uitableviewcommonprotocol = uitableviewdelegate &Uitableviewdatasourceclasstestdelegate:uitableviewcommonprotocol{Func TableView (_ Tableview:uitableview, Numberofrowsinsection section:i NT)-Int {return 0} func TableView (_ Tableview:uitableview, Cellforrowat Indexpath:indexpath)-UITableViewCell {Let cell= Tableview.dequeuereusablecell (withidentifier:"Cell")        returnCell}}

When defining a protocol, you can use Associatedtype to implement generics

Associatedtype Use instances

protocol associatedtypetestprotocol{    associatedtype T    func AppendData (_ data:[t])    func ResetData (_ Data:[t])}class  associatedtypetestview:associatedtypetestprotocol{        //  Specify the specific type     when implementing the Protocol}//  must be consistent with the type of the method above     }}

Ficow Original, reproduced please specify the source:http://www.cnblogs.com/ficow/p/8227701.html

Swift Typealias Associatedtype

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.