storing images and strings in a Dictionary in Swift 3

Discussion in 'iOS Programming' started by theprizerevealed, Oct 31, 2016.

  1. theprizerevealed macrumors member

    Joined:
    Feb 26, 2016
    #1
    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?
     
  2. chown33, Nov 1, 2016
    Last edited: Nov 1, 2016

    chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    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.
     
  3. theprizerevealed, Nov 1, 2016
    Last edited by a moderator: Nov 2, 2016

    theprizerevealed thread starter macrumors member

    Joined:
    Feb 26, 2016
    #3
    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?
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    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.

    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.
     
  5. loon3y macrumors 65816

    loon3y

    Joined:
    Oct 21, 2011
    #5
    Is storing images in a dictionary better than Core Data?
     
  6. theprizerevealed thread starter macrumors member

    Joined:
    Feb 26, 2016
    #6
    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.
    --- Post Merged, Nov 2, 2016 ---
    I'm a novice to Swift 3 and iOS so I'm not yet familiar with Core Data.
     
  7. 1458279 Suspended

    1458279

    Joined:
    May 1, 2010
    Location:
    California
    #7
    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.
     

Share This Page