UIImage (named : ""\(UserDefaults.standard.string(forKey: "value") ?? "")") doesn't match

Hi everyone,

I will try to explain my problem and what I already tried

I have a Controller where I chose 2 images with a picker view. For each picker view I save the name of the image (from assets) in a Label. Then, I save the label.text in a like that :

UserDefaults.standard.set(Exo_1_Label.text, forKey: "Nom_Exo_1_Jour_1_Programme_1") UserDefaults.standard.set(Exo_2_Label.text, forKey: "Nom_Exo_2_Jour_1_Programme_1")

For this part is ok because when I use these exercise names in another controller view is working. But, my problem is that when I want to use these names to call the image in UIImageViews it doesn't work.

Image_Exo01.image = UIImage (named: "(UserDefaults.standard.string(forKey: "Nom_Exo_1_Jour_1_Programme_1") ?? "")")

            Image_Exo02.image = UIImage (named: "(UserDefaults.standard.string(forKey: "Nom_Exo_2_Jour_1_Programme_1") ?? "")")

For the first image, it work but not for the second.. I don't understand because the value of the second Userdefaults is right because I use it for the title of the controller.. I tried to use the name of the image and it working, so I think the problem is from to use twice the UIImage (named : ...) with userdefaults in same time

Maybe someone knows what is my problem...

Answered by DTS Engineer in 726706022

This is a bit puzzling:

Image_Exo01.image = UIImage (named: "(UserDefaults.standard.string(forKey: "Nom_Exo_1_Jour_1_Programme_1") ?? "")") // <-- #1

Is that really your code? It looks like you mean:

Image_Exo01.image = UIImage (named: UserDefaults.standard.string(forKey: "Nom_Exo_1_Jour_1_Programme_1") ?? "") // <- #2

or maybe:

Image_Exo01.image = UIImage (named: "\(UserDefaults.standard.string(forKey: "Nom_Exo_1_Jour_1_Programme_1") ?? "")") // <-- #3

There's really no point to #3 in this case, since it's equivalent to #2 but harder to read.

Firstly, I wouldn't use this: Image_Exo01.image = UIImage (named: "(UserDefaults.standard.string(forKey: "Nom_Exo_1_Jour_1_Programme_1") ?? "")") (specifically: ?? ""). What you're saying there is, try and get a UIImage for this string value, but if that string value doesn't work out, try and get me a UIImage for the image named "". Determine whether the image exists before attempting to fetch it.

Secondly, why do you need it as a UIImage? If the image is in your assets can't you use Image("myImage")?

Hello,

Thank you for taking time to answer me !

1: I understand but, normally the text saved in the UserDefaults.standard.string(forKey: "Nom_Exo_1_Jour_1_Programme_1" is the exact image name from the asset. Because when the user choose the image, the label associated is exactly the good name. The problem is that in the same view I read the good text from this Userdefault, so I don't understand why in the first UIImage (named : ) is working but not in the second
2: In fact, I can't use the Image ("myImage") because I can't know what the image that the user will choose. My objectif it was to use the text according the image chosen by the user.

Sorry for my English and programmation level.. Thanks again for your time

  1. The text stored in the UserDefaults key will be the name of whatever image you've stored in there, so it'll be your asset image name, I agree, but my point is that your code is going to either return the image name OR it's going to return "" (because you've given the default image name of ""), and that isn't going to give you an image.

Check that the key is populated before you try to create an image from it:

let image1Name = UserDefaults.standard.string(forKey: "Nom_Exo_1_Jour_1_Programme_1") ? ""
let image2Name = UserDefaults.standard.string(forKey: "Nom_Exo_2_Jour_1_Programme_1") ? ""

if(image1Name != "") {
    Image_Exo01.image = UIImage(named: image1Name)
}
if(image2Name != "") {
    Image_Exo02.image = UIImage(named: image2Name)
}

Do the images the user is selecting actually exist in your assets?

  1. You misunderstood. I wasn't saying to use an image called "myImage", I was saying that you could use Image() rather than UIImage(), but if you need a UIImage, go for it.
Accepted Answer

This is a bit puzzling:

Image_Exo01.image = UIImage (named: "(UserDefaults.standard.string(forKey: "Nom_Exo_1_Jour_1_Programme_1") ?? "")") // <-- #1

Is that really your code? It looks like you mean:

Image_Exo01.image = UIImage (named: UserDefaults.standard.string(forKey: "Nom_Exo_1_Jour_1_Programme_1") ?? "") // <- #2

or maybe:

Image_Exo01.image = UIImage (named: "\(UserDefaults.standard.string(forKey: "Nom_Exo_1_Jour_1_Programme_1") ?? "")") // <-- #3

There's really no point to #3 in this case, since it's equivalent to #2 but harder to read.

UIImage (named : ""\(UserDefaults.standard.string(forKey: "value") ?? "")") doesn't match
 
 
Q