Book Image

Swift Cookbook

By : Cecil Costa, Cecil Costa
Book Image

Swift Cookbook

By: Cecil Costa, Cecil Costa

Overview of this book

Table of Contents (18 chapters)
Swift Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Receiving JSONs


Asking for remote information is something very common today. What happens if your playground finished before you receive a server response? In this recipe, we will learn how to deal with this problem

Getting ready

Create a new playground called Chapter 6 Requesting JSONs, and just in this case, check your internet connection.

How to do it...

To receive JSONs on your playground, follow these steps:

  1. First, we need to locate a URL, which can return more websites. In this case, we will use this URL: https://api.github.com/users/mralexgray/repos.

  2. Create a constant with the URL mentioned earlier:

    let url = NSURL(string: "https://api.github.com/users/mralexgray/repos")!
  3. Click on the quick look icon and check that you receive a JSON response.

    Tip

    You can use the quick look icon with NSURL for checking websites. If you are testing something, which is modifying a website, you can use quick look to check the new website look.

  4. Now, let's create a request for our URL:

    let request = NSURLRequest...