TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: Day 50: Milestone: Projects 10-12

Forums > 100 Days of Swift

Took a while to figure out how to have the DetailViewController show the selected image. The solution was quite simple, and took me a couple of hours and many 'print()' to figure out.

All I had to do was to add URL....

class Image: NSObject, NSCoding {
    var name: String
    var url: URL

    init(name: String, url: URL){
        self.name = name
        self.url = url
    }

    required init?(coder decoder: NSCoder){
        name = decoder.decodeObject(forKey: "name") as? String ?? ""
        url = decoder.decodeObject(forKey: "image") as? URL ?? URL(fileReferenceLiteralResourceName: "")
    }

    func encode(with coder: NSCoder) {
        coder.encode(name, forKey: "name")
        coder.encode(url, forKey: "url")
    }
}
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if let dvc = storyboard?.instantiateViewController(identifier: "ImageView") as? DetailViewController {
            dvc.selectedImageURL = images[indexPath.row].url
            dvc.selectedImageName = images[indexPath.row].name
            navigationController?.pushViewController(dvc, animated: true)
        }
    }

3      

Hacking with Swift is sponsored by String Catalog.

SPONSORED Get accurate app localizations in minutes using AI. Choose your languages & receive translations for 40+ markets!

Localize My App

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.