user2636197
user2636197

Reputation: 4122

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?

Upvotes: 2

Views: 6096

Answers (1)

vadian
vadian

Reputation: 285069

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
}

Upvotes: 3

Related Questions