Kiwo Tew
Kiwo Tew

Reputation: 1531

cannot assign a value of type json to a value of type NSArray

I am trying to send some info with a segue but I get the following error cannot assign a value of type json to a value of type NSArray

When I try to do:

self.pageImages = apiResult["images"]

I have apiResult setup as:

var apiResult: JSON! = []

Then I'm using alamofire together with swiftyjson in order to fetch the result and cast it to json, but now I need it as an NSArray.

My data is returned as:

{
    "somedata": {
        some info
    },
    "someData": {
        some info..
    },
    "images": [
       "URL1", "URL2", "URL3"
    ]
}

Upvotes: 2

Views: 3359

Answers (2)

Eric Aya
Eric Aya

Reputation: 70097

If apiResult is the JSON object returned by SwiftyJSON and if apiResult["images"] is the array of URLs you want to assign to your pageImages array variable, then you have to use the array property of the JSON typed SwiftyJSON object:

self.pageImages = apiResult["images"].array

Upvotes: 3

Mark
Mark

Reputation: 12535

Your array definition is setup to accept value of type JSON - what you want is to accept value of type [JSON] - i.e., an array of JSON values.

var apiResult: [JSON!] = []

Additionally, I'm not sure why you'd need implicitly unwrapped optionals unless you have a good reason for it?

Also, Swift has type inference. So if your values being returned are an array of JSON objects, then the compiler knows that and you don't have to explicitly tell it a value type.

Upvotes: -1

Related Questions