类型[SubscriptType]不符合协议StringLiteralConvertible错误

时间:2015-04-08 06:39:58

标签: swift

类型[SubscriptType]不符合协议StringLiteralConvertible

// JsonRequest.swift

class JsonRequest {
var title: String?
var postBody: String?
var coverImage: String?

init(json: NSDictionary){
    self.title = json["title"] as? String
    self.postBody = json["body"] as? String
    self.coverImage = json["img_url"] as? String
}
}

// ViewController.swift file

var posts = [JsonRequest]()

let feedUrl = NSURL(string: "http://example.com/json")
    // 2
    if let JSONData = NSData(contentsOfURL: feedUrl!) {
        // 3
        var jsonResult = NSJSONSerialization.JSONObjectWithData(JSONData, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary

        var myJSON = JSON(jsonResult)
        let arrayLength = myJSON["dump"].array?.count

        if arrayLength != 0 {
            for postIndex in 0...arrayLength! - 1 {
                var postArray = myJSON["dump"][postIndex]["title"] as? [NSDictionary]
                for item in postArray {
                    posts.append(JsonRequest(json: item))
               }
            }
        }
    }

我想从我的JSON ["dump"][0, 1, 2]["title"]追加到postArray array,将所有这些标题保存在此数组中,但这是错误。如何修复它并将我的标题保存在此数组中?

1 个答案:

答案 0 :(得分:1)

您无法将postArray转换为[NSDictionary],因为它不是NSDictionary。 但它是字符串,这里是你的示例代码。

var posts = [String]()

    let feedUrl = NSURL(string: "http://example.com/en/feed")
    // 2
    if let JSONData = NSData(contentsOfURL: feedUrl!) {
        // 3
        var jsonResult = NSJSONSerialization.JSONObjectWithData(JSONData, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary

        var myJSON = JSON(data:JSONData)
        let arrayLength = myJSON["dump"].array?.count

        if arrayLength != 0 {
            for postIndex in 0...arrayLength! - 1 {
                var post = myJSON["dump"][postIndex]["title"].stringValue
                posts.append(post)
                println(posts)
            }
        }
    }

修改

以这种方式更新您的代码:

<强> JsonRequest.swift

class JsonRequest {
var title: String?
var postBody: String?
var coverImage: String?

init(json: JSON){
    self.title = json["title"].stringValue
    self.postBody = json["body"].stringValue
    self.coverImage = json["img_url"].stringValue
    }
} 

<强> ViewController.swift

var posts = [JsonRequest]()

    let feedUrl = NSURL(string: "http://example.com/en/feed")
    // 2
    if let JSONData = NSData(contentsOfURL: feedUrl!) {
        // 3
        var jsonResult = NSJSONSerialization.JSONObjectWithData(JSONData, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary

        var myJSON = JSON(data:JSONData)
        let arrayLength = myJSON["dump"].array?.count
        var dict = myJSON["dump"]

        if arrayLength != 0 {
            for postIndex in 0...arrayLength! - 1 {
                var tempDict = dict[postIndex]
                posts.append(JsonRequest(json: tempDict))
            }
        }
    }

希望它有所帮助。