Dynamically check if an object supports dynamic member lookup

Trenskow

Is there a way in Swift 4.2 to tell if an arbitrary object supports dynamic member lookup?

Something like the example below.

func getName(of object: AnyObject) -> String? {
    if let object = object as? DynamicMemberLookupProtocol {
        return object["name"]
    }
    return nil
}
pckill

I do not think this is possible at the moment.

However, after SR-8077 Compile error when using @dynamicMemberLookup as a protocol requirement bug gets fixed in a production version of swift (hopefully, in swift 5), we can have a workaround for that in the form of protocols, constrained by @dynamicMemberLookup.

You can also read here on why @dynamicMemberLookup was not implemented as a protocol:

We started with the approach of making this be a protocol that types conform to to get this behavior. It turns out that this behavior is very non-protocol like: it is not useful to define generic algorithms over, and existential values are only useful if they define a specific subscript that implements the requirements implicit in this attribute.

For these and other reasons, defining this as a protocol doesn't really fit into the design of Swift.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Echo object member of object member

分類Dev

How to check whether GNU Make supports Guile

分類Dev

How to check if device supports face authentication for BiometricPrompt

分類Dev

Check which languages an application supports programmatically

分類Dev

Check checkbox in a list dynamically

分類Dev

SSIS Lookup Expression for Formatting a Date in Dynamic SQL

分類Dev

How to create an object dynamically?

分類Dev

How to find the object that a member belongs to

分類Dev

Pointer to rvalue object member variable

分類Dev

object johnsnowlabs is not a member of package com

分類Dev

Sort depending on member function of the object

分類Dev

How can I check if a name server supports DNSSEC?

分類Dev

How to check if a given driver kernel module supports a given device?

分類Dev

How to check if a FTP server supports EPSV (Extended Passive) mode

分類Dev

Typing dynamic object fields

分類Dev

Runtime dynamic for object property

分類Dev

Type trait: Check if reference member variable is static or not

分類Dev

C++ class member check if not a template

分類Dev

Share member variables with member object for API backwards compatibility during refactor

分類Dev

Dynamically create conditionals from dynamic dataset in JavaScript

分類Dev

how to dynamically import components with react ? / Dynamic import

分類Dev

Change locale dynamically according to a dynamic currency value

分類Dev

How to update the state of an object dynamically?

分類Dev

Add select object dynamically to table

分類Dev

Dynamically set object property value

分類Dev

How to check if an object is nullable?

分類Dev

How to check if an object is nullable?

分類Dev

Accessing the properties of an object of an object dynamically in PHP

分類Dev

Assigning values to constant member of dynamically allocated struct during initialization

Related 関連記事

  1. 1

    Echo object member of object member

  2. 2

    How to check whether GNU Make supports Guile

  3. 3

    How to check if device supports face authentication for BiometricPrompt

  4. 4

    Check which languages an application supports programmatically

  5. 5

    Check checkbox in a list dynamically

  6. 6

    SSIS Lookup Expression for Formatting a Date in Dynamic SQL

  7. 7

    How to create an object dynamically?

  8. 8

    How to find the object that a member belongs to

  9. 9

    Pointer to rvalue object member variable

  10. 10

    object johnsnowlabs is not a member of package com

  11. 11

    Sort depending on member function of the object

  12. 12

    How can I check if a name server supports DNSSEC?

  13. 13

    How to check if a given driver kernel module supports a given device?

  14. 14

    How to check if a FTP server supports EPSV (Extended Passive) mode

  15. 15

    Typing dynamic object fields

  16. 16

    Runtime dynamic for object property

  17. 17

    Type trait: Check if reference member variable is static or not

  18. 18

    C++ class member check if not a template

  19. 19

    Share member variables with member object for API backwards compatibility during refactor

  20. 20

    Dynamically create conditionals from dynamic dataset in JavaScript

  21. 21

    how to dynamically import components with react ? / Dynamic import

  22. 22

    Change locale dynamically according to a dynamic currency value

  23. 23

    How to update the state of an object dynamically?

  24. 24

    Add select object dynamically to table

  25. 25

    Dynamically set object property value

  26. 26

    How to check if an object is nullable?

  27. 27

    How to check if an object is nullable?

  28. 28

    Accessing the properties of an object of an object dynamically in PHP

  29. 29

    Assigning values to constant member of dynamically allocated struct during initialization

ホットタグ

アーカイブ