使用 SwiftyJSON,数组不能将类型 AnyObject 的值分配给类型 String

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

我有这个变量:

var fetchedImagesArray: [String] = []

然后我使用 Alamofire 和 SwiftyJson 从我的服务器获取图像名称数组,如下所示:

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

fetchedImagesArray = fetchedImages

}

但我在这里得到错误 fetchedImagesArray = fetchedImages 说:无法将类型 anyobject 的值分配给类型字符串。

返回的数组看起来像这样 ["imgName1","imgName2","imgName3"] 这都是字符串,为什么我不能设置 fetchedImagesArray?

SwiftJSON 属性 arrayObject returns [AnyObject]? 所以你必须将数组向下转换为它的实际类型

if let fetchedImages = json["images"].arrayObject as? [String] {
    fetchedImagesArray = fetchedImages
}