External image in array from one VController to another, keep getting (Unresolved identifier)

I am trying to pass an image from one ViewController to another but I keep getting an unresolved identifier error, the images are coming from an external URL. I am sure it is because I am not calling the correct variable? I have been using multiple tutorials that use local images but not external and cannot seem to get passed this step. Any and all comments/advice very welcome (please be gentle though as I am very new to Swift).

    //  ViewController.swift
    //  CustomTableView

    import UIKit
    import Alamofire
    import AlamofireImage

    class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

        let URL_GET_DATA = "http://www.localnewsplus.com.au/ios/service.php"

        @IBOutlet weak var tableViewHeroes: UITableView!

        var heroes = [Hero]()

        public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
            return heroes.count
        }

        public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{

            let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ViewControllerTableViewCell

            let hero: Hero
            hero = heroes[indexPath.row]

            cell.labelName.text = hero.name
            cell.labelTeam.text = hero.team

            Alamofire.request(hero.imageUrl!).responseImage { response in

                if let image = response.result.value {
                    cell.heroImage.image = image
                }
            }

            return cell
        }

        override func viewDidLoad() {
            super.viewDidLoad()

            Alamofire.request(URL_GET_DATA).responseJSON { response in

                if let json = response.result.value {

                    let heroesArray : NSArray  = json as! NSArray

                    for i in 0..<heroesArray.count{

                        self.heroes.append(Hero(
                            name: (heroesArray[i] as AnyObject).value(forKey: "st_heading") as? String,
                            team: (heroesArray[i] as AnyObject).value(forKey: "st_modified") as? String,
                            imageUrl: (heroesArray[i] as AnyObject).value(forKey: "imageurl") as? String
                        ))

                    }

                    self.tableViewHeroes.reloadData()
                }

            self.tableViewHeroes.reloadData()

            }

        }

        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            let vc = storyboard?.instantiateViewController(withIdentifier: "articleViewController") as? articleViewController
            vc?.image = UIImage(named: Hero[indexPath.row])!
            //vc?.name = name[indexPath.row]
            self.navigationController?.pushViewController(vc!, animated:true)
        }

        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
        }

    }




//  articleViewController.swift
//  CustomTableView

import UIKit

class articleViewController: UIViewController {

    @IBOutlet weak var img: UIImageView!
    @IBOutlet weak var lbl: UILabel!
    @IBOutlet weak var text: UITextView!

    var image = UIImage()
    var name = ""

    override func viewDidLoad() {
        super.viewDidLoad()

        lbl.text = name
        img.image = image
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

}

1 answer

  • answered 2018-08-09 00:26 ukim

    Undefined Identifier is a compiler error, so it does not depend on any runtime error (for example, url). It means you are calling a variable/class/method by wrong name.

    The problematic line is:

    vc?.image = UIImage(named: Hero[indexPath.row])!
    

    You should pass a String to the method, but what is Hero[indexPath.row]? Hero is a Class name, instead of an array. I guess you want something like

    vc?.image = UIImage(named: heros[indexPath.row].imageUrl)!