NSObject does not have a member named Key error
Asked Answered
S

1

0

I need to construct a JSON object in the format shown below.

{
  "DeviceCredentials": {
    "UniqueId": "sample string 1"
  },
  "Handovers": [
    {
      "Occasions": [
        {
          "Id": 1
        },
        {
          "Id": 1
        }
      ]
    }
  ]
}

There is a issue though with the Occasions array though. The number of Occasion objects vary. Sometimes there can be only one Occasion, sometimes multiple. Since this is dynamic I wrote the below method to create this array.

func getOccasionArray(handover: Handover) -> JSON {
    var occArray = [[String: NSNumber]]()
    for occ in handover.occasions {
        let occasion = occ as Occasion
        var obj = ["Id": occasion.id!]
        occArray.append(obj)
    }
    let json = JSON(occArray)
    return json
}

I use the SwiftyJSON library to convert the created array to JSON. Here's the returned result of json variable looks like for a Handover which has only one Occasion.

[
  {
    "Id" : 243468
  }
]

So far so good. The problem is I'm having trouble plugging this into the bigger JSON response. Simply calling the method like this gives the error 'NSObject' does not have a member named 'Key'.

var parameters = [String: NSObject]()

parameters = [
    "DeviceCredentials": [
        "UniqueId": "1212121212"
    ],
    "Handovers": [
        [
            "Occasions": getOccasionArray(handover)
        ]
    ]
]

I tried converting the returned value to an array getOccasionArray(handover).arrayValue but no change.

Any idea on how to fix this?

Thanks.

Salts answered 26/2, 2015 at 10:7 Comment(0)
I
2

You don't need SwiftyJSON here then:

Make your 'getOccasionArray' to return a [String: NSObject]

func getOccasionArray(handover: Handover) -> [String: NSObject] {
  var occArray = [[String: NSNumber]]()
  for occ in handover.occasions {
    let occasion = occ as Occasion
    var obj = ["Id": occasion.id!]
    occArray.append(obj)
  }
  return occArray
}

Define parameters as [String: NSObject]:

var parameters = [[String: NSObject]]()

var json: [String: NSObject] = getOccasionArray(handover)        

parameters = [["DeviceCredentials": ["UniqueId": "1212121212"], "Handovers": ["Occasions": json]]]
Illstarred answered 26/2, 2015 at 10:39 Comment(3)
Unfortunately I cannot have the parameters variable's type as JSON. You see I need to pass this JSON object as a parameter for a URL request. I use Alamofire for HTTP request handling and it only accepts parameter in [String: AnyObject] format.Salts
oh ok, it should probably work if you leave it as: [String: NSObject] as long as you change getOccasionArray to return that too. You can stay away from swiftyJSON for that case then. I edited the post aboveIllstarred
Thank you. That worked! Although I had to change the getOccasionArray method's return type to [[String: NSObject]] and occArray's type to [[String: NSObject]](). Now everything's working fine. Thank you.Salts

© 2022 - 2024 — McMap. All rights reserved.