This demonstration shows:
-
How to use the ObjectMapper tool that converts between JSON and models
This README describes how to create the demo.
To use this demo, you can clone this repo, or you can use this README to create your own project.
If you clone this repo, then be aware that there are multiple git branches, so pick the one you want.
-
swift-4-xcode-9: Swift version 4, Xcode version 9, iOS version 11.
-
swift-3-xcode-8: Swift version 3, Xcode version 8, iOS version 10.
Launch Xcode and create a new Xcode project.
-
Use iOS template "Single View Application" and Product Name "Demo Swift ObjectMapper"
Create a simple way to print some text to the screen.
-
We create a text view object and IBOutlet named "demoTextView".
Add ObjectMapper to the project. We suggest using Carthage or Cocoapods.
-
Carthage
Cartfile
:github "Hearst-DD/ObjectMapper" ~> 2.2
Create a new group named "Models".
Create a new iOS Swift file named "Item.swift", and when you save it, also create a new disk folder named "Models".
Edit Item.swift
.
import Foundation
import ObjectMapper
class Item: Mappable {
var name: String?
// Implement Mappable
required init?(map: Map) {
}
// Implement Mappable
func mapping(map: Map) {
name <- map["name"]
}
}
Edit ViewController.swift
.
Add simple code to create a model object, then print some output to the screen.
import UIKit
import ObjectMapper
class ViewController: UIViewController {
@IBOutlet weak var demoTextView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
let item = Mapper<Item>().map(JSONString: "{\"name\":\"Demo Item\"}")
demoTextView.text = item!.name!
}
…
Run the app.
The Simulator screen shows the item name, which is "Demo Item".
Congratulations, you're successful!
- Package: demo_swift_objectmapper
- Version: 3.0.0
- Created: 2016-05-30
- Updated: 2017-09-22
- License: BSD, GPL, MIT
- Contact: Joel Parker Henderson (http://joelparkerhenderson.com)