GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

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 Alex.

SPONSORED Alex is the iOS & Mac developer’s ultimate AI assistant. It integrates with Xcode, offering a best-in-class Swift coding agent. Generate modern SwiftUI from images. Fast-apply suggestions from Claude 3.5 Sonnet, o3-mini, and DeepSeek R1. Autofix Swift 6 errors and warnings. And so much more. Start your 7-day free trial today!

Try for free!

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.