title: Display image from URL, Swift 4.2
body: <p>I am a fairly decent Objective C developer, and I am now learning Swift (of which I am finding quite difficult, not only because of new concepts, such as optionals, but also because Swift is continually evolving, and much of the available tutorials are severely outdated).</p>
  
  <p>Currently I am trying parse a JSON from a url into an NSDictionary and then use one of its value to display an image (which is also a url). Something like this:</p>
  
  <hr>
  
  <p>URL -> NSDictionary -> init UIImage from url -> display UIImage in UIImageView</p>
  
  <hr>
  
  <p>This is quite easy in Objective C (and there may even be a shorter answer):</p>
  
  <pre><code>NSURL *url = [NSURL URLWithString:@"https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY"];
  NSData *apodData = [NSData dataWithContentsOfURL:url];
  NSDictionary *apodDict = [NSJSONSerialization JSONObjectWithData:apodData options:0 error:nil];
  </code></pre>
  
  <p>The above code snippet gives me back a standard NSDictionary, in which I can refer to the "url" key to get the address of the image I want to display:</p>
  
  <hr>
  
  <p>"url" : "<a href="https://apod.nasa.gov/apod/image/1811/hillpan_apollo15_4000.jpg" rel="nofollow noreferrer">https://apod.nasa.gov/apod/image/1811/hillpan_apollo15_4000.jpg</a>"</p>
  
  <hr>
  
  <p>This I then convert into a UIImage and give it to a UIImageView:</p>
  
  <pre><code>NSURL *imageURL = [NSURL URLWithString: [apodDict objectForKey:@"url"]];
  NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
  UIImage *apodImage = [UIImage imageWithData:imageData];
  
  UIImageView *apodView = [[UIImageView alloc] initWithImage: apodImage];
  </code></pre>
  
  <p>Now, I am basically trying to replicate the above Objective C code in Swift but continuously run into walls. I have tried several tutorials (one of which actually did the exact same thing: display a NASA image), as well as find a few stack overflow answers but none could help because they are either outdated or they do things differently than what I need.</p>
  
  <p>So, I would like to ask the community to provide the Swift 4 code for the these problems:</p>
  
  <pre><code>1. Convert data from url into a Dictionary
  2. Use key:value pair from dict to get url to display an image
  </code></pre>
  
  <p>If it is not too much already, I would also like to ask for detailed descriptions alongside the code because I would like the answer to be the one comprehensive "tutorial" for this task that I believe is currently not available anywhere.</p>
  
  <p>Thank you!</p>