How to use dictionary types as generic constraints in Swift

Today, the group participants Someone made such a problem,Why use the characteristic dictionary type as a generic type constraint,It will inevitably error?

Using a specific dictionary type as a generic type constraint will report an error



Obviously,He said very clearly,"You can not be from a non-agreement、Non-type class inheritance. "。Obviously,The dictionary is a generic structure...

Then the solution is also very clear ideas,Creating a class to decorate a dictionary might be a good choice but too much trouble,Then start from the agreement。

Conditional agreement to follow

We can extend this structure to the dictionary,Let us follow a protocol of empty:

But here we note qualification,That dictionary Key must be a String:

All content is empty,Because without doing any extra work。

Such,We can use this protocol to constrain,If the type of dictionary does not meet the requirements,Then the dictionary will not abide by our agreement:

Test Results

 

Original article written by Gerber drop-off:R0uter's Blog » How to use dictionary types as generic constraints in Swift

Reproduced Please keep the source and description link:https://www.logcg.com/archives/3101.html

About the Author

R0uter's Blog

The non-declaration,I have written articles are original,Reproduced, please indicate the link on this page and my name。

Comments

Leave a Reply

Your email address will not be published.