Post

Replies

Boosts

Views

Activity

Set NSImage to variable instead of quoted string for image name
Thanks in advance to all. I am sure I am doing something stupid here. This is a macOS project and I am trying to update an image view using a variable, not the quoted name of the image as done here: self.myAppIcon.image = NSImage(named:"microsoft.png")That works fine and the app will show the default image on load and then change to the named image ("microsoft.png") when requested. However, when I try to get it to use a variable in the place of the quoted string, I am not getting the app to update to image.I assumed I could just use something like: self.myAppIcon.image = NSImage(named: self.theIcon)where theIcon is set by loading a string from a file read from disk. All the images are in the project and all load is called explicitly. Here I am reading from a file to get the image name: let myIcon = try String(contentsOf: iconURL, encoding: .utf8) self.theIcon = myIconWhen I print myIcon I see the value being read properly from the text file. Changing the text file will show the new value on next call. However, the image view does not update. I've tried quite a few variations but either I get a class mismatch (image not string, data not string, etc), or I thow execution errors.How do I reference a variable to allow the NSImage to update based on a dynamic value? Isn't NSImage(named: String) a string value? Shouldn't I be able to replace that quoted String with a variable that is a string?Appreciate any help. I suspect this is a remarkably simple solution but I am too deep into the forest to see the trees at this point and likely need to step away.
4
0
1.3k
Feb ’20