Demo Swift REST
Swift Other
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
Demo Swift REST
.gitignore
README.md

README.md

Demo Swift REST

REST

This is a demonstration of:

This README describes how to create the project, if you want to try doing it yourself.

How to create the project

  1. Launch Xcode and create a new project. We call ours "Demo Swift REST".

  2. Create a simple way to print some text to the screen, such as a text view with an IBOutlet named "demoTextView".

  3. Add dependencies for Alamofire, ObjectMapper, and Realm.

    • To add these, we prefer using Carthage.

    • If you prefer, you can add it by using a dynamic framework or using Cocoapods.

    • Need help with Carthage? See our repo demo_swift_carthage.

  4. If you want a simpler introduction to each piece of this demonstration, see these related repos:

Add Alamofire

  1. Edit ViewController.swift.

  2. Add Alamofire networking code:

    import UIKit
    import Alamofire
    
    class ViewController: UIViewController {
    
      @IBOutlet weak var demoTextView: UITextView!
    
      override func viewDidLoad() {
        super.viewDidLoad()
    
        Alamofire.request(.GET, "https://httpbin.org/get")
          .validate()
          .responseString { response in
             self.demoTextView.text = response.result.value
           }
         }
      }
      …
    }
    
  3. Verify Alamofire works by runing the app. The screen shows the response result value string, which looks something like this.

    {
      "args": {},
      "headers": {
        "Accept": "*/*",
        "Accept-Encoding": "gzip;q=1.0, compress;q=0.5",
        "Accept-Language": "en-US;q=1.0",
        "Host": "httpbin.org",
        "User-Agent": "MyApp/com.example.MyApp …"
      },
      "origin": "207.237.149.238",
      "url": "https://httpbin.org/get"
    }
    

Add an ObjectMapper model class

  1. Create a directory named "Models".

  2. Create a model called "Item" that implements the ObjectMappable interface:

    import ObjectMapper
    
    class Item: Mappable {
    
      // Create some properties that correspond to the
      // key fields in the JSON data that we will fetch.
      var origin: String?
      var url: String?
    
      // Implement Mappable
      required init?(_ map: Map) {
      }
    
      // Implement Mappable
      func mapping(map: Map) {
        origin <- map["origin"]
        url <- map["url"]
      }
    
    }
    

Instantiate an ObjectMapper model instance

  1. Edit ViewController.swift.

  2. Add simple code to create a model object, then print some output to the screen.

    import UIKit
    import Alamofire
    import ObjectMapper
    
    class ViewController: UIViewController {
    
      @IBOutlet weak var demoTextView: UITextView!
    
      override func viewDidLoad() {
        super.viewDidLoad()
    
        Alamofire.request(.GET, "https://httpbin.org/get")
          .validate()
          .responseString { response in
            let item = Mapper<Item>().map(response.result.value)
            self.demoTextView.text = item!.url!
          }
        }
      }
      …
    }
    
  3. Run the app. The screen shows the item URL, which is "https://httpbin.org/get".

Upgrade the model to use Realm

  1. Edit Models/Item.swift.

  2. Add code to import RealmSwift, and add public init methods, and make the properties dynamic and default to nil.

    import Alamofire
    import ObjectMapper
    import RealmSwift
    
    public class Item: Object, Mappable {
    
      // Create some properties that correspond to the
      // key fields in the JSON data that we will fetch.
      dynamic var origin: String? = nil
      dynamic var url: String? = nil
    
      // Realm init
      convenience public init(data: [String: AnyObject]) {
        self.init()
      }
    
      // Implement Mappable
      required convenience public init?(_ map: Map) {
        self.init()
      }
    
      // Implement Mappable
      func mapping(map: Map) {
        origin <- map["origin"]
        url <- map["url"]
      }
    
    }
    

Upgrade the view to use Realm

  1. Edit ViewController.swift

  2. Add code to open Realm, and write and item, and read an item.

    import UIKit
    import Alamofire
    import ObjectMapper
    import RealmSwift
    
    class ViewController: UIViewController {
    
      @IBOutlet weak var demoTextView: UITextView!
    
      override func viewDidLoad() {
        super.viewDidLoad()
    
        let realm = try! Realm()
        Alamofire.request(.GET, "https://httpbin.org/get")
          .validate()
          .responseString { response in
            let item = Mapper<Item>().map(response.result.value)
    
            // Write the item to the database
            try! realm.write {
              realm.add(item!)
            }
    
            // Call and safely unwrap the url from the database, then assign to the textView
            if let url = realm.objects(Item).first?.url {
                self.demoTextView.text = url
            }
        }
      }
      …
    }
    
  3. Run the app. The screen shows the item URL, which is "https://httpbin.org/get".

Tracking

  • Package: demo_swift_rest
  • Version: 1.0.9
  • Created: 2016-05-30
  • Updated: 2016-08-11
  • License: BSD, GPL, MIT
  • Contact: Joel Parker Henderson (joel@joelparkerhenderson.com)