【问题标题】:Using SwiftyJSON, array cannot assign value of type AnyObject to type String使用 SwiftyJSON,数组无法将 AnyObject 类型的值分配给 String 类型
【发布时间】:2017-01-21 06:08:57
【问题描述】:

我有这个变量:

var fetchedImagesArray: [String] = []

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

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

fetchedImagesArray = fetchedImages

}

但我在这里得到错误 fetchedImagesArray = fetchedImages 说:不能将任何对象类型的值分配给字符串类型。

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

【问题讨论】:

  • 你试过if let fetchedImages = json["images"].arrayObject as [String]

标签: ios arrays swift alamofire swifty-json


【解决方案1】:

SwiftJSON 中,属性arrayObject 返回[AnyObject]?,因此您必须将数组向下转换为其实际类型

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-24
    相关资源
    最近更新 更多