I'm trying to show a random image into a view controller, although the function of creating the random image is in another swift-file (and it should be like that!). Now I'm getting the error: "Unexpectedly found nil while unwrapping an Optional value" when I'm assigning the random image to the image view of the viewcontroller (line 3 of the function 'createRandomOutfit()'. I honestly don't know why and how I should solve this. I hope someone can help me!
p.s. I also added the images into the same folder as the view controller.
//
// Wardrobe.swift
// WhatToWear
//
// Created by issd on 06/04/2018.
// Copyright © 2018 1. All rights reserved.
//
import Foundation
var allCoats = ["coat3.jpg", "coat6.jpg", "coat12.jpg", "coat21.jpg"]
var allSweaters = ["sweater1.jpg", "sweater5.jpg", "sweater10.jpg", "sweater21.jpg"]
var allTrousers = ["trousers7.jpg", "trousers11.jpg", "trousers14.jpeg", "trousers24.jpeg"]
var allShoes = ["shoe5.png", "shoe10.jpg", "shoe16.jpg", "shoe25.jpg"]
var outfitViewController = ViewController()
func createRandomOutfit() -> String {
var outfitResult = Outfit(coat: nil, sweater: nil, trousers: nil, shoes: nil)
let randomCoat = Int(arc4random_uniform(UInt32(allCoats.count)))
outfitViewController.coatImage.image = UIImage(named: allCoats[randomCoat])
print(allCoats[randomCoat])
let randomSweater = Int(arc4random_uniform(UInt32(allSweaters.count)))
outfitViewController.sweaterImage.image = UIImage(named: allSweaters[randomSweater])
print(allSweaters[randomSweater])
let randomTrousers = Int(arc4random_uniform(UInt32(allTrousers.count)))
outfitViewController.trousersImage.image = UIImage(named: allTrousers[randomTrousers])
print(allTrousers[randomTrousers])
let randomShoes = Int(arc4random_uniform(UInt32(allShoes.count)))
outfitViewController.shoeImage.image = UIImage(named: allShoes[randomShoes])
print(allShoes[randomShoes])
//outfitViewController.shoeImage = UIImageView(image: UIImage(named: allShoes[randomShoes]))
outfitResult = Outfit(coat: allCoats[randomCoat], sweater: allSweaters[randomSweater], trousers: allTrousers[randomTrousers], shoes: allShoes[randomShoes])
return String(describing: outfitResult)
}
Aucun commentaire:
Enregistrer un commentaire