Search code examples
iosswiftinitializer-list

Cannot find an initializer for type UIImage that accepts an argument list of type string


Currently, this code is working , but paris is a fixed value, i would like to input a variable, and define it just before.

let entry1 = data.paris[indexPath.row]
let  image = UIImage(named: entry1.filename)
cell2.bkImageView2.image = image
cell2.headingLabel2.text = entry1.heading

But when i do it like this just below an error appears. Cannot find an initializer for type UIImage that accepts an argument list of type (named:string?!)

let key1 = "paris"
let entry1 = data[key1][indexPath.row]
let  image = UIImage(named: entry1.filename)
cell2.bkImageView2.image = image
cell2.headingLabel2.text = entry1.heading

Have you any information to help me ?

Edit : 29 July 13h12

Ok, Following your explains, i have this :

var entry = data[key][indexPath.row]
if let imgName = entry1.filename {
            let  image = UIImage(named: imgName!)
            cell2.bkImageView2.image = image
            cell2.headingLabel2.text = entry1.heading
        }

But now, i have still a error on last sentence (cell2.headinglabel...) : Ambiguous use of "heading"

Any idea ? But for the previous request, perfect ! Great job ! :)


Solution

  • It means that entry1.filename is optional string, not string, try this:

    if let imgName = entry1.filename {
        let  image = UIImage(named: imgName)
    }
    

    Just remember that image is optional as well.