Swift how to use a dictionary as a paradigm constraints

Today, the group participants Someone made such a problem,Why dictionary type characteristics as the paradigm of the time constraint type,It will inevitably error?

With a particular type of dictionary constraint as given type paradigm

Obviously,He said very clearly,"You can not be from a non-agreement、Non-type class inheritance. "。Obviously,A dictionary is a paradigm 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


Published by R0uter

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

Leave a comment

Your email address will not be published. Required fields are marked *