ios - Could not cast value of type 'SwiftyJSON.JSON' to 'Swift.String' -
i have been trying display data parsed json uitableview
. used swiftyjson
parse data , pass array controller.
i used nsarray
force json array. when print passed data, shows following:
( "string one", "string two" )
then inside the
func collectionview(_ collectionview: uicollectionview, cellforitemat indexpath: indexpath) -> uicollectionviewcell
function, used
let urls: string = sshostring[indexpath.item] as! string
when print array inside function cellforitemat
without forcing type string, prints following:
string 1 string 2
but when force type string crashes , shows following:
could not cast value of type 'swiftyjson.json' 'swift.string'.
how make work? how force string?
as requested:
var featuredscreenshots:nsarray = nsarray() var featuredssho = nsmutablearray() items in json["featured"].arrayvalue { featuredssho.add(items["screenshots"].arrayvalue) } self.featuredscreenshots = featuredssho nsarray
then passed other controller:
let vc = self.storyboard?.instantiateviewcontroller(withidentifier: "detailsview") as! detailsview vc.sshostring = featuredscreenshots[indexpath.row] as! nsarray
accessing element of json in swiftyjson returns type json
.
in order type looking for, need use included properties of json type so:
let urls: string = sshostring[indexpath.item].string!
with swiftyjson .string
of optional type string?
, why left force unwrap there. in general, force unwraps should avoided, recommend doing along these lines:
if let urls = sshostring[indexpath.item].string { //urls type string, , can use needed }
i recommend reviewing documentation swiftyjson, goes more detail , shows other examples using .int
, .bool
, .url
, etc values looking for.
Comments
Post a Comment