user2636197 user2636197 - 1 year ago 140
Swift Question

Using SwiftyJSON, array cannot assign value of type AnyObject to type String

I have this variable:

var fetchedImagesArray: [String] = []

Then I fetch an array of images name from my server using Alamofire and SwiftyJson like this:

if let fetchedImages = json["images"].arrayObject {

fetchedImagesArray = fetchedImages


But I get the error here fetchedImagesArray = fetchedImages saying: cannot assign value of type anyobject to type string.

The array returned loooks like this ["imgName1","imgName2","imgName3"] which is all strings to why cant I set fetchedImagesArray?

Answer Source

In SwiftJSON the property arrayObject returns [AnyObject]? so you have to downcast the array to its actual type

if let fetchedImages = json["images"].arrayObject as? [String] {
    fetchedImagesArray = fetchedImages
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download