How to use optional chaining while searching through a dictionary in swift?

Jacky Wang

I want to safely search through values in a swift Dictionary using if lets and making sure it is type safe as I get deeper and deeper into the dictionary. The dictionary contains dictionaries that contains NSArray that contains more dictionary.

At first attempt my code looks like this:

  if let kkbox = ticket["KKBOX"] as? Dictionary<String, AnyObject> {
        if let kkboxDlUrlDict = kkbox["kkbox_dl_url_list"] as? Dictionary<String, AnyObject> {
            if let kkboxDlUrlArray = kkboxDlUrlDict["kkbox_dl_url"] as? NSArray {
                for dict in kkboxDlUrlArray {
                    if let name = dict["name"] as? String {
                        if name == mediaType.rawValue {
                            urlStr = dict["url"] as String
                        }
                    }
                }
            } else { return nil }
        } else { return nil }
    } else { return nil }

How do I shorten it to perhaps one or 2 line?

I realised I can chain it if it is 2 layers. This works:

 if let kkboxDlUrlArray = ticket["KKBOX"]?["kkbox_dl_url_list"] as? NSArray {

    }

But any chain longer than that, will not compile.

Is there a way to chain through a dictionary more than once?

Thank you

Jacky Wang

Seems like swift 1.2 has added this feature.

"More powerful optional unwrapping with if let — The if let construct can now unwrap multiple optionals at once, as well as include intervening boolean conditions. This lets you express conditional control flow without unnecessary nesting."

https://developer.apple.com/swift/blog/

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to use optional chaining with array in Typescript?

From Dev

Swift 3.0 Optional Chaining

From Dev

swift optional chaining with cast

From Dev

Optional chaining with Swift strings

From Dev

Optional chaining and Array in swift

From Dev

Optional Chaining or ternary expression in Swift?

From Java

How to use optional chaining in Node.js 12

From Dev

How to use regular expression while searching in HashSet

From Dev

an unexpected error while writing a piece of code from Swift programming by Apple(optional chaining)

From Dev

How to use Dictionary in While Loop?

From Dev

Swift dictionary and Optional interaction

From Dev

Swift optional chaining doesn't work in closure

From Dev

Is there something like the swift optional chaining in javascript?

From Dev

Optional chaining used in left side of assignment in Swift

From Dev

Accessing boolValue in a NSNumber var with optional chaining (in Swift)

From Dev

Searching through objects in array swift

From Dev

Java stream: use optional filter() operations on chaining

From Dev

js: proper way to use optional chaining?

From Dev

Searching a dictionary in a list through a flask API

From Dev

How to use optional binding in Swift 2

From Java

Iterating Through a Dictionary in Swift

From Dev

Loop through a dictionary in swift

From Dev

Chaining UIButton click through several delegates in Swift

From Dev

Assign value to optional dictionary in Swift

From Dev

Accessing optional values of a Swift dictionary

From Dev

How to know where Optional Chaining is breaking?

From Dev

how to extend optional chaining for all types

From Dev

Swift - How Can I use 'shouldChangeTextInRange' with Firebase for real time searching?

From Dev

Swift optional operator use

Related Related

  1. 1

    How to use optional chaining with array in Typescript?

  2. 2

    Swift 3.0 Optional Chaining

  3. 3

    swift optional chaining with cast

  4. 4

    Optional chaining with Swift strings

  5. 5

    Optional chaining and Array in swift

  6. 6

    Optional Chaining or ternary expression in Swift?

  7. 7

    How to use optional chaining in Node.js 12

  8. 8

    How to use regular expression while searching in HashSet

  9. 9

    an unexpected error while writing a piece of code from Swift programming by Apple(optional chaining)

  10. 10

    How to use Dictionary in While Loop?

  11. 11

    Swift dictionary and Optional interaction

  12. 12

    Swift optional chaining doesn't work in closure

  13. 13

    Is there something like the swift optional chaining in javascript?

  14. 14

    Optional chaining used in left side of assignment in Swift

  15. 15

    Accessing boolValue in a NSNumber var with optional chaining (in Swift)

  16. 16

    Searching through objects in array swift

  17. 17

    Java stream: use optional filter() operations on chaining

  18. 18

    js: proper way to use optional chaining?

  19. 19

    Searching a dictionary in a list through a flask API

  20. 20

    How to use optional binding in Swift 2

  21. 21

    Iterating Through a Dictionary in Swift

  22. 22

    Loop through a dictionary in swift

  23. 23

    Chaining UIButton click through several delegates in Swift

  24. 24

    Assign value to optional dictionary in Swift

  25. 25

    Accessing optional values of a Swift dictionary

  26. 26

    How to know where Optional Chaining is breaking?

  27. 27

    how to extend optional chaining for all types

  28. 28

    Swift - How Can I use 'shouldChangeTextInRange' with Firebase for real time searching?

  29. 29

    Swift optional operator use

HotTag

Archive