Swift as, as!, as? Differentiate between t.type and dynamic types

Source: Internet
Author: User

As

1. The compiler checks the legality of the type conversion; static

Let cell = Collectionview.dequeuereusablecell (Withreuseidentifier:shoppinglist[indexpath.section], For:indexpath as Indexpath)

Let k = cell as Indexpath

Cannot convert value of type ' Uicollectionviewcell ' to type ' indexpath ' in coercion

2, with switch: type detection

as! As?

Type dynamic transformation;

Convert any type to a specific type, type system check.

When a type is a function parameter, it belongs to the dynamic type;

Type systems and compilers are closely related.

When initializing an instance with a type, the compiler requires that the type qualification must be static, as opposed to a function call.

Therefore, the type parameter that is passed as a type must be qualified for initialization of the instance.

(Type as! Hmloginmodel.type). Init ()

Extension Observabletype where E = = Response {

Public Func mapmodel<t:handyjson> (_ Type:t.type), observable<t> {

Return FLATMAP {response-observable<t> in

(Type as! Hmloginmodel.type). Init ()

Return Observable.just (Response.mapmodel (t.self))

}

}

}

Swift as, as!, as? Differentiate between t.type and dynamic types

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.