# Swift 4 and Alamofire - Type 'DataResponse' has no subscript members

**URL:** <https://forums.kodeco.com/t/swift-4-and-alamofire-type-dataresponse-has-no-subscript-members/40164>\
**Category:** Swift\
**Created:** [March 9, 2018, 2:13pm UTC](https://forums.kodeco.com/t/swift-4-and-alamofire-type-dataresponse-has-no-subscript-members/40164 "2018-03-09T14:13:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![trifek](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/trifek/32/102075_2.png) [@trifek](https://forums.kodeco.com/u/trifek)\
**Post date:** [March 9, 2018, 2:13pm UTC](https://forums.kodeco.com/t/swift-4-and-alamofire-type-dataresponse-has-no-subscript-members/40164/1 "2018-03-09T14:13:53Z")

</div>

Hi,  
I have this code:

```
let params = ["parm": "getList",
                      "obj": "none"]

    Alamofire.request("http://nazwa.pl/", method: .get, parameters: params).responseJSON { responseData in
        if responseData.result.isSuccess {
            if((responseData.result.value) != nil) {
                let swiftyJsonVar = JSON(responseData.result.value!)

                var result = responseData
                print("result: \(responseData)")

                if let imieINazwisko = result["imieINazwisko"] as? String {
                    print("Imię i nazwisko: \(imieINazwisko)")
                }

            } else {
            }
        } else {
        }
    }

```

And my data:

```
{
  "ranking": {
    "id": 50971,
    "nazwa": "grupowy",
    "opis": "cx",
    "typ": "GRUPA",
    "dataWidoczneOd": {
      "year": 2018,
      "month": 2,
      "dayOfMonth": 1,
      "hourOfDay": 0,
      "minute": 0,
      "second": 0
    },
    "dataWidoczneDo": {
      "year": 2018,
      "month": 2,
      "dayOfMonth": 31,
      "hourOfDay": 0,
      "minute": 0,
      "second": 0
    },
    "dataOd": {
      "year": 2018,
      "month": 2,
      "dayOfMonth": 1,
      "hourOfDay": 0,
      "minute": 0,
      "second": 0
    },
    "dataDo": {
      "year": 2018,
      "month": 2,
      "dayOfMonth": 31,
      "hourOfDay": 0,
      "minute": 0,
      "second": 0
    },
    "idkiPlikowGrafiki": [
      "50976"
    ],
    "grupy": [
      {
        "id": 50981,
        "idkiPlikowGrafiki": [
          "50983",
          "50986"
        ],
        "kod": "ttt",
        "nazwa": "ttt",
        "gracze": []
      },
      {
        "id": 51032,
        "idkiPlikowGrafiki": [
          "51034"
        ],
        "kod": "yyy",
        "nazwa": "yyy",
        "gracze": [
          {
            "czasGry": "0 min",
            "liczbaZdobytychPunktow": "0.0",
            "email": "pawrilo@gmail.com",
            "zdjecieZFacebooka": "https://graph.facebook.com/1018655608235134/picture?width=200&height=150",
            "imieINazwisko": "Paweł Opała",
            "zakonczonaGra": false
          },
          {
            "czasGry": "0 min",
            "liczbaZdobytychPunktow": "0.0",
            "email": "lukasz.peta@icloud.com",
            "zdjecieZFacebooka": "https://graph.facebook.com/201192440635362/picture?width=200&height=150",
            "imieINazwisko": "Łukasz Betta",
            "zakonczonaGra": false
          }
        ]
      }
    ]
  }
}

```

i have problem with error. error: Type ‘DataResponse’ has no subscript members?

How can I repair it?

---

<div class="post-metadata">

**Author:** ![7stud](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/7stud/32/81494_2.png) [@7stud](https://forums.kodeco.com/u/7stud)\
**Post date:** [March 9, 2018, 3:58pm UTC](https://forums.kodeco.com/t/swift-4-and-alamofire-type-dataresponse-has-no-subscript-members/40164/2 "2018-03-09T15:58:16Z")

</div>

```
import UIKit

class DataResponse: CustomStringConvertible {

    var x = "{\"a\": 1, \"b\": 2}"

    var description: String {
        return x
    }

    func dostuff() {
        print("doing stuff")
    }
}

let dr = DataResponse()
print(dr)

```

Output:

`{"a": 1, "b": 2}`

Whoo! Hooo! That looks like a python dictionary (or ruby hash, or javascript object). Let’s subscript it!

`print(dr["a"])`

Output:

> error: type ‘DataResponse’ has no subscript members

Lesson:

1. Swift isn’t Python (or Ruby, or Javascript). In Swift, dictionaries look like this: `["b": 2, "a": 1]`

2. And strings aren’t dictionaries anyway:

Wooo! Hoo! That looks like a Swift dictionary. Let’s subscript it:

`print(str["a"])`

Output:

> error: cannot subscript a value of type ‘String’ with an index of type ‘String’

---

<div class="post-metadata">

**Author:** ![syedfa](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/syedfa/32/101500_2.png) [@syedfa](https://forums.kodeco.com/u/syedfa)\
**Post date:** [April 1, 2018, 12:42am UTC](https://forums.kodeco.com/t/swift-4-and-alamofire-type-dataresponse-has-no-subscript-members/40164/3 "2018-04-01T00:42:41Z")

</div>

@trifek Thanks very much for your question!

I think the problem is with the following line:

`let swiftyJsonVar = JSON(responseData.result.value!)`

Try changing it to:

`let swiftyJsonVar = JSON(responseData.result.value!) as? [String: Any]`

I would also encourage you to look at the following [tutorial](https://www.raywenderlich.com/172145/encoding-decoding-and-serialization-in-swift-4) on our site on parsing JSON using Swift 4’s new Codable protocol. It really makes things a lot easier, and eliminates the need to use any third party libraries for JSON parsing 🙂

I hope this helps!

All the best!

---

<div class="post-metadata">

**Author:** ![jayantvarma](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jayantvarma/32/111768_2.png) [@jayantvarma](https://forums.kodeco.com/u/jayantvarma)\
**Post date:** [April 4, 2018, 9:55pm UTC](https://forums.kodeco.com/t/swift-4-and-alamofire-type-dataresponse-has-no-subscript-members/40164/4 "2018-04-04T21:55:23Z")

</div>

@trifek,  
Do not get confused with all of the off-topic chatter. The answer to your problem lies with what @syedfa has suggested.

For an explanation on why that is the case,  
In Swift you have a variable of type Any and it can hold literally any type, which is a good thing and a not so good thing because there is no automatic conversion. So when dealing with JSON, you have Array, Dictionary, Strings, Int, etc but unless specified they are Any, so you have to downcast them into a specific type.

A dictionary in swift is also called a key value pair, where again the key is a String where as the value could be anything (Any).

So you have to first convert the responseData into a dictionary with  
`let swiftyJsonVar = JSON(responseData.result.value!) as? [String:Any]`

now you can access the swiftyJsonVar as a swift dictionary, if it is not castable into a dictionary as it could be an array, then it will fail so you must use proper guard or if statements to avoid errors.

cheers,

Jayant

---

<div class="post-metadata">

**Author:** ![system](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@system](https://forums.kodeco.com/u/system)\
**Post date:** [August 23, 2018, 6:26am UTC](https://forums.kodeco.com/t/swift-4-and-alamofire-type-dataresponse-has-no-subscript-members/40164/5 "2018-08-23T06:26:48Z")

</div>

This topic was automatically closed after 166 days. New replies are no longer allowed.
