0

I am building an array that will display images in a UITableView. When I try to call the image in the array it doesn't find the asset. What am I doing wrong?

We've tried renaming all of the variables / arrays.

var videos: [Video] = []

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.

    self.view.backgroundColor = UIColor .darkmodeGray
}

func createArray() -> [Video] {

    var tempVideos: [Video] = []

    let video1 = Video(image: WeekOne, title: "StoryOne")
    let video2 = Video(image: UIImage, title: "StoryTwo")
    let video3 = Video(image: WeekThree, title: "StoryThree")
    let video4 = Video(image: WeekFour, title: "StoryFour")
    let video5 = Video(image: WeekFive, title: "StoryFive")
    let video6 = Video(image: WeekSix, title: "StorySix")

    tempVideos.append(video1)
    tempVideos.append(video2)
    tempVideos.append(video3)
    tempVideos.append(video4)
    tempVideos.append(video5)
    tempVideos.append(video6)

    return tempVideos

}


// IN A SEPERATE FILE

import Foundation
import UIKit

class Video {

    var image: UIImage
    var title: String

    init(image: UIImage, title: String)    {
        self.image = image
        self.title = title
    }
}

Two different errors based on the text after image:

Use of unresolved identifier 'WeekOne'

and

Cannot convert value of type 'UIImage.Type' to expected argument type 'UIImage'

Sweeper
  • 213,210
  • 22
  • 193
  • 313
Paul Drees
  • 73
  • 1
  • 5
  • Why did you expect it to work? What is `WeekOne`, `WeekThree` etc? Can you show their declarations? – Sweeper Nov 03 '19 at 19:00
  • Those are the names of the image files in the assets. They are .png files. – Paul Drees Nov 03 '19 at 19:03
  • You can't just use their names directly. Does [this](https://stackoverflow.com/questions/32152197/how-to-load-image-from-images-xcassets-into-a-uiimage-with-swift/32152965) answer your question? – Sweeper Nov 03 '19 at 19:04
  • Also, `UIImage` is the name of a data type rather than a variable, which is why you get the 2nd error. – Phillip Mills Nov 03 '19 at 19:07
  • I am using this code in a separate swift file. I thought it would take the image and put it into the cell in my tableview. I looked at your link but wasn't able to figure out where/what I need to add to my code. class StoryCell: UITableViewCell { @IBOutlet weak var storyImageView: UIImageView! } – Paul Drees Nov 03 '19 at 19:19

2 Answers2

0

If I understand your requirement correctly, then here's what you are looking for.

let video1 = Video(image: UIImage(imageLiteralResourceName: "WeekOne"), title: "StoryOne")

You need to your the above initializer of UIImage to access an image added to Assets.

The above line of code will be translated to this by your Xcode.

enter image description here

let video2 = Video(image: UIImage, title: "StoryTwo")

If 'UIImage' is an image name, I'd kindly suggest you change it as it may lead to ambiguity. I guess you made a typo there. You typed UIImage instead of WeekTwo

Also, make sure that you have added corresponding images (WeekOne, WeekTwo, WeekThree, WeekFour, WeekFive, WeekSix) to Assets

0

To really answer this we'd need to see what WeekOne is. The error suggests that you haven't defined WeekOne. The issue with that error lies there.

The second error is a typo in your Video 2 line. You have UIImage (a class) when you want a variable name instead.

let video1 = Video(image: WeekOne, title: "StoryOne")
let video2 = Video(image: UIImage, title: "StoryTwo") 

Additionally, convention is to make make variable names start with a lower-case letter and all class names start with uppercase letters. WeekOne, WeekTwo, etc would more properly named weekOne, weekTwo, etc. This makes it easier to see at a glance what kind of thing each name is.

Dancreek
  • 9,524
  • 1
  • 31
  • 34