我想将我的json文件中的图片解析为我的SubViewController,但我不知道该怎么做。我试图做点什么,但似乎这是错的,请你帮我。这是我的JSON文件:
{"Name":"Fruit1",
"Picture":"http://www.starpropertiesindia.com/blog/wp-content/uploads/2016/08/kochi1.jpg",
"Description":"red"
},
我想将它们转换为SubViewController,我得到另一个错误:
无法指定NSURL类型的值来输入UIimage!在那一行:
let ImageView = fruits[indexpath.row].imageURL
VC .SentData3 = ImageView
如果这可以帮助你我在开头创建了一个结构:
struct Fruit {
let name : String
let imageURL : NSURL
let description : String
}
答案 0 :(得分:0)
我不熟悉parsing JSON,但您可以尝试这种方式。
func parseJSON() -> Fruit {
var json: Array!
do {
json = try NSJSONSerialization.JSONObjectWithData(JSONData, options: NSJSONReadingOptions()) as? Array
} catch {
print(error)
}
guard let item = json[0] as? [String: AnyObject],
let name = item["Name"] as? String,
let pictureString = item["Picture"] as? String,
let description = item["Description"] as? String else {
return
}
return Fruit(name, imageURL: NSURL(string: pictureString), description: description)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let myFruit = parseJSON()
if segue.identifier == "yourSegueIdentifier" {
if let vc = segue.destinationViewController as? SubViewController {
let data = NSData(contentsOfURL: myFruit.imageURL!)
vc.imageView.image = UIImage(data: data!)
}
}
}