Неожиданно обнаружен ноль при развертывании необязательного значения изображения - PullRequest
0 голосов
/ 08 сентября 2018

Конечный контроллер

class CharDetailsViewController: UIViewController {

    @IBOutlet weak var imageView: UIImageView!
    @IBOutlet weak var nameLabel: UILabel!
    @IBOutlet weak var genderLabel: UILabel!
    @IBOutlet weak var houseLabel: UILabel!
    @IBOutlet weak var ancestryLabel: UILabel!

    var image = UIImage()
    var name = String()
    var gender = String()
    var house = String()
    var ancestry = String()

    override func viewDidLoad() {
        super.viewDidLoad()
        imageView.image = image
        nameLabel.text! = name
        houseLabel.text! = house

        // Do any additional setup after loading the view.
    }

Контроллер источника

class CharacterTableViewController: UITableViewController {

    var charactersData = [Character]()
    override func viewDidLoad() {
        super.viewDidLoad()
        loadData()
    }

    func loadData()
    {
        DispatchQueue.main.async {
            Alamofire.request("http://hp-api.herokuapp.com/api/characters").responseJSON(completionHandler: {
                (response) in
                switch response.result
                {
                case.success(let value):
                    let json = JSON(value)
                    print(json)
                    json.array?.forEach({
                        (character) in
                        let character = Character(name: character["name"].stringValue, house:character["house"].stringValue,image:character["image"].stringValue, gender: character["gender"].stringValue, ancestry: character["ancestry"].stringValue)
                        self.charactersData.append(character)
                    })
                    self.tableView.reloadData()
                case.failure(let error):
                    print(error.localizedDescription)
                }
            })
        }
    }
override func tableView( tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return charactersData.count
    }


    override func tableView( tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CharTableViewCell

        cell.nameLabel.text = "Name: " +  charactersData[indexPath.row].name
        cell.houseLabel.text = "House: " + charactersData[indexPath.row].house

        if let imageURL = URL(string: self.charactersData[indexPath.row].image) {
            DispatchQueue.global().async {
                let data = try? Data(contentsOf: imageURL)
                if let data = data {
                    let image = UIImage(data: data)
                    DispatchQueue.main.async {
                        cell.charImageView.image = image
                    }
                }
            }
        }
        return cell
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        let hpc = storyboard?.instantiateViewController(withIdentifier: "CharDetails") as? CharDetailsViewController

        hpc?.image = UIImage(named: charactersData[indexPath.row].image)! //Error found here

        hpc?.name = charactersData[indexPath.row].name
        hpc?.house = charactersData[indexPath.row].house
        self.navigationController?.pushViewController(hpc!, animated: true)
    }

class Character
{
    var name : String
    var house : String
    var image : String


    init(name: String, house: String, image: String)
    {
        self.name = name
        self.house = house
        self.image = image
    }
}

Я получаю неожиданно найденный ноль при развертывании изображения необязательного значения для передачи изображения на другой контроллер. Я проверил розетки, и они в порядке. Изображения извлекаются из массива URL в формате JSON, как показано выше, они отлично работают в табличном представлении и отображаются в табличном представлении, но не отображаются на странице подробного представления. Пожалуйста, помогите мне.

1 Ответ

0 голосов
/ 08 сентября 2018

Вы пытаетесь принудительно развернуть UIImage, который равен нулю.UIImage(named:) метод используется, когда изображение присутствует внутри пакета, и вы хотите показать его оттуда.

hpc?.image = UIImage(named: charactersData[indexPath.row].image)! //Error found here

В приведенной выше строке вы используете UIImage (named :)!Это означает, что вы принудительно распаковываете его, что приводит к падению с неожиданно найденным нулем при развертывании необязательного значения

Решение Вы можете передать имя изображения так же, какимя и дом,

1) Обновите

class CharDetailsViewController: UIViewController {
  // Add this property to hold url string for image. 
  var imageUrlString = String()
}

2) в didSelectRow, обновите код для передачи imageUrlString

hpc?.imageUrlString = charactersData[indexPath.row].image

2) поместите это в свой метод viewDidLoad CharDetailsViewController

if let imageURL = URL(string: imageUrlString) {
    DispatchQueue.global().async {
        let data = try? Data(contentsOf: imageURL)
        if let data = data {
            let image = UIImage(data: data)
            DispatchQueue.main.async {
                self.imageView.image = image
            }
        }
    }
}

Надеюсь, это поможет

...