0

I have one array of png images.

iconeImage = [UIImage(named:"ion-home")!,UIImage(named:"ms-medkit")!,UIImage(named:"ion-edit")!,UIImage(named:"ion-clipboard")!,UIImage(named:"ion-clipboard")!,UIImage(named:"ion-waterdrop")!,UIImage(named:"ion-calendar")!]

and I have one function for table-view which set the image with this array elements

here is the code

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "MenuTableViewCell") as! MenuTableViewCell
        cell.imgIcon.image = iconeImage[indexPath.row]
        return cell
    }

But it gives me error that iconeImage has nil values. so can you suggest me solutions. I am beginner in swift 3

Here is the screenshot a busy cat ![two muppets][1]

sanjay rathod
  • 77
  • 1
  • 2
  • 16
  • Do you really have an image named `ion-home` in your project? Same for each one? – Larme Aug 10 '17 at 12:25
  • It seems like you don't actually have images matching all those image names or you might not be storing them in the correct place. Unless they are part of `Assets.xcassets`, you need to specify the extension in the image name as well. – Dávid Pásztor Aug 10 '17 at 12:27
  • If it says that `iconeImage` is `nil`, or that it has `0` images, it means that the code above has never been executed, or that the content was replaced in some other part. If on the other hand, one of the image inits should return nil, the app would crash saying that you had an unexpected nil (because of the forced unwrapps "!"). – Daniel Aug 10 '17 at 12:27
  • yes i have that images in my Assets.xcassets folder – sanjay rathod Aug 10 '17 at 12:28
  • Just for suggestion : Instead of Using Array of images why dont you just use Array of image name ? – Devang Tandel Aug 10 '17 at 12:32

4 Answers4

4

Instead of holding all images in memory I'd suggest you to store image names:

let iconImages = ["ion-home", "ms-medkit", "ion-edit", "ion-clipboard", "ion-waterdrop", "ion-calendar"]

And preload each image directly when you need it. It will not affect on performance as it is small icons. Also code will look cleaner.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "MenuTableViewCell") as! MenuTableViewCell
    cell.imgIcon.image = UIImage(named: iconImages[indexPath.row])
    return cell
}

Double check if all the assets are added to your project. If they are there, image should be displayed.

Try to avoid of using implicitly unwrapped optional (!) in your code. Nil value will crash the app. In your example, imageView.image is an optional, so you can set UIImage(named: "imageName") to it.

Also, you can try to clean build folder, clean project and reinstall the app. Sometimes copying resources failed in Xcode.

Stas Volskiy
  • 441
  • 2
  • 5
  • check if Assets.xcassets added to your target. Open assets file, in the right menu tap on 1 tab. Check Target Membership section, there should be checkmark near name of your target – Stas Volskiy Aug 10 '17 at 13:26
2

Please try the below format

  let img = iconeImage[indexPath.row]
    if let imgData = img {
        cell.imgView.image = UIImage(data:imgData)
    }
Ved Rauniyar
  • 1,539
  • 14
  • 21
2

You can store your image names instead:

let iconImageNames = ["ion-home","ms-medkit", ... , "ion-calendar")]

Later access them in your method:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "MenuTableViewCell") as! MenuTableViewCell
    let name = iconeImage[indexPath.row] ?? yourDefaultImageName
    cell.imgIcon.image = UIImage(named: name)
    return cell
}

As an example

Vladyslav Zavalykhatko
  • 15,202
  • 8
  • 65
  • 100
1

I don't know exactly what is the issue.

But when i have searching over stackoverflow. somewhere i have read that something problem with Assets.xcassets

then i got the error exact of this

And then solve the error with the help of that answer.

and hopefully i resolved the error.

meanwhile the problem is with assets folder.

sanjay rathod
  • 77
  • 1
  • 2
  • 16