0

I'm trying to pass the value at the indexpath to the next view controller but I'm unsure on how to do that.

var imageStore =  [Data]()
var imageStoreReference = [(resultResponse, Data)]()


 func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let DestVC = self.navigationController?.storyboard?.instantiateViewController(withIdentifier: "DetailViewController") as! DetailViewController
        let filtered = imageStoreReference.filter({imageStore.contains($0.1)}).first
        DestVC.createdAt = filtered?.0.createdAt
        DestVC.Imagedescription = filtered?.0.description
        DestVC.instagram = filtered?.0.user.instagram
        DestVC.twitter = filtered?.0.user.twitter
        DestVC.portfolio = filtered?.0.user.portfolio
        DestVC.fullImage = filtered?.0.urls.regular
        DestVC.userProfileImage = filtered?.0.user.profileImageUrl.regular
}

Here is resultResponse, that is referenced in the tuple of imageStoreReference.

struct resultResponse: Codable {
    let createdAt: String
    let description: String?
    let urls: urlResponse
    let user: userResponse
  }

struct urlResponse: Codable {
    let regular: String
    let small: String
}

struct userResponse: Codable {
    let instagram: String?
    let twitter: String?
    let name:String?
    let portfolio: String?
    let profileImageUrl: imageSize
    
    enum CodingKeys: String, CodingKey {
        case instagram = "instagram_username"
        case twitter = "twitter_username"
        case profileImageUrl = "profile_image"
        case name
        case portfolio = "portfolio_url"
    }
}

struct imageSize: Codable {
    let regular: String?
}
Sola
  • 25
  • 6
  • You posted code that creates a new VC, DestVC. You cast it to the desired class, DetailViewController, and then set various properties on it. You then forget about the newly created view controller. You should probably add code to push that new view controller onto the navigation stack. – Duncan C Jul 14 '20 at 15:44
  • If you want to pass the model data for the selected item to your detail view controller, give it a property for the model object, and pass it the model data (fetch the object at the specified indexPath.) We don't know what your data model looks like, so we can't tell you exactly how to do that. – Duncan C Jul 14 '20 at 15:45
  • @DuncanC I had a pushViewController, but I accidentally left it out when I pasted the code. I was planning on sending each variable instead. My data model is a struct of JSON objects. – Sola Jul 14 '20 at 21:39

1 Answers1

1

You should create a variable which has type as "resultResponse" in DestVC.

Example:

class DestVC: UIViewController {
    var filered: resultResponse?
}

In CollectionView you need only pass filtererd variable. It make your code clean. And you should use "if let ... " to ensure your app cant crash when data nil

Example:

var imageStore =  [Data]()
var imageStoreReference = [(String, Data)]()


 func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let DestVC = self.navigationController?.storyboard?.instantiateViewController(withIdentifier: "DetailViewController") as! DetailViewController
    if let index = imageStoreReference.firstIndex(where: { (image) -> Bool in
        imageStore.contains(image.1)
    })
        let filtered = imageStoreReference[index]
        DestVC.filtered = filtered
}
HHumorous
  • 174
  • 1
  • 11