【问题标题】:Type [SubscriptType] does not conform to protocol StringLiteralConvertible error类型 [SubscriptType] 不符合协议 StringLiteralConvertible 错误
【发布时间】:2015-04-08 06:39:58
【问题描述】:

类型[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,将所有这些标题保存在这个数组中,但这里是这个错误。如何修复它并将我的标题保存在这个数组中?

【问题讨论】:

    标签: swift


    【解决方案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))
                }
            }
        }
    

    希望对你有帮助。

    【讨论】:

    • 在这一行posts.append(post) 打印下一个错误:'NSString' is not a subtype of 'JsonRequest'
    • 这只是我采用字符串数组的示例,但您可以根据需要对其进行修改。
    • 我也添加了我的JsonRequest 文件。可能对你有帮助
    猜你喜欢
    • 1970-01-01
    • 2014-12-06
    • 1970-01-01
    • 1970-01-01
    • 2015-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多