Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

theprizerevealed

macrumors regular
Original poster
Feb 26, 2016
183
12
I am trying to store images and strings inside a Dictionary in the manner UIImage: String. The UIImage would be the key and the String would be the value. There are several key:value pairs and each key and each value are unique.

I want to take a certain, single key (which I won't know in advance) and extract from that dictionary the corresponding value.

After extracting that value from the key:value pair of that dictionary then I want to store that single value in a variable as a string.

Then I want to compare that variable to another variable containing a string.

I'm experiencing difficulty because the comparisons never seem to work. There should be comparisons that correspond as boolean True.

Is there any flaw to what I wish to attempt? Am I using the Swift Dictionary correctly?
 
Post your code.
Even if it doesn't work.
Especially if it doesn't work.

You've described what you want it to do.
That description seems plausible.
Now you need to show the code you wrote for it.

We have to see the description you wrote for the computer.
That's what code is: a description written for the computer.
 
Last edited:
for example:
Code:
var dictionary: [UIImage: String] = [UIImage(named: “firstpicture”)!: “firstpicture”, UIImage(named: “secondpicture”)!: “secondpicture”, UIImage(named: “thirdpicture”)!: “thirdpicture”]
In this code, what would be the key? would it be "firstpicture"? or UIImage(named: “firstpicture”)!

Is there any way to grab only the part “firstpicture” from the key?
 
Last edited by a moderator:
First, please use CODE tags when posting code.

Second, which one do you think is the key? If you're not sure, you should be able to refer to the documentation and come up with a definite answer.

Is there any way to grab only the part “firstpicture” from the key?
Your original description said this:
The UIImage would be the key and the String would be the value.​

So based on this description, you'd use a UIImage as the key, and the returned value would be a String.

You didn't describe what UIImage you were going to use as the key for the Dictionary, nor where it came from.

You also wrote this in your original description:
After extracting that value from the key:value pair of that dictionary then I want to store that single value in a variable as a string.

Then I want to compare that variable to another variable containing a string.

I'm experiencing difficulty because the comparisons never seem to work. There should be comparisons that correspond as boolean True.​

You haven't posted any code to "extract a value from ... the dictionary".
You haven't posted any code to "store that ... in a variable as a string".
You haven't posted any code to "compare that variable to another variable".
In other words, you haven't posted complete code for the original description you gave.

You said you were having difficulty with the comparison. You didn't say you were having difficulty with everything.
 
I don't intend to post my specific code; in any case I have found a workaround. However if anyone else knows how to properly access a Dictionary Key that uses
Code:
UIImage(named: “firstpicture”)!: “firstpicture”
so I might access an associated value - why then I'd be much obliged. However, if someone knows definitively that it could not be done that way, with a supporting source then I would be glad to know that also.
[doublepost=1478083085][/doublepost]
Is storing images in a dictionary better than Core Data?
I'm a novice to Swift 3 and iOS so I'm not yet familiar with Core Data.
 
  • Like
Reactions: twatson
I've written code and have read articles from others about storing pictures inside of other data stores like Core Data and others.

Basically, there was no valid reason to store pics inside of Core Data. What I do is store the name of the file and then load the file. You can setup a directory for specific pics.

I guess if you have to do something special with the pic, there might be a reason for storing it inside of another system, but generally storing the name, then loading from that name works very well.

Remember, the directory is actually a database system, as is the whole file system.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.