swift - Type [SubscriptType] does not conform to protocol StringLiteralConvertible error -



swift - Type [SubscriptType] does not conform to protocol StringLiteralConvertible error -

type [subscripttype] not conform protocol 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]() allow feedurl = nsurl(string: "http://example.com/json") // 2 if allow jsondata = nsdata(contentsofurl: feedurl!) { // 3 var jsonresult = nsjsonserialization.jsonobjectwithdata(jsondata, options: nsjsonreadingoptions.mutablecontainers, error: nil) nsdictionary var myjson = json(jsonresult) allow arraylength = myjson["dump"].array?.count if arraylength != 0 { postindex in 0...arraylength! - 1 { var postarray = myjson["dump"][postindex]["title"] as? [nsdictionary] item in postarray { posts.append(jsonrequest(json: item)) } } } }

i want append json["dump"][0, 1, 2]["title"] postarray array, save titles in array, here error. how can prepare , save titles in array?

you can not cast postarray [nsdictionary] because not nsdictionary. string , here illustration code you.

var posts = [string]() allow feedurl = nsurl(string: "http://example.com/en/feed") // 2 if allow jsondata = nsdata(contentsofurl: feedurl!) { // 3 var jsonresult = nsjsonserialization.jsonobjectwithdata(jsondata, options: nsjsonreadingoptions.mutablecontainers, error: nil) nsdictionary var myjson = json(data:jsondata) allow arraylength = myjson["dump"].array?.count if arraylength != 0 { postindex in 0...arraylength! - 1 { var post = myjson["dump"][postindex]["title"].stringvalue posts.append(post) println(posts) } } }

edit

update code way:

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]() allow feedurl = nsurl(string: "http://example.com/en/feed") // 2 if allow jsondata = nsdata(contentsofurl: feedurl!) { // 3 var jsonresult = nsjsonserialization.jsonobjectwithdata(jsondata, options: nsjsonreadingoptions.mutablecontainers, error: nil) nsdictionary var myjson = json(data:jsondata) allow arraylength = myjson["dump"].array?.count var dict = myjson["dump"] if arraylength != 0 { postindex in 0...arraylength! - 1 { var tempdict = dict[postindex] posts.append(jsonrequest(json: tempdict)) } } }

hope helps.

swift

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -