Science Score: 26.0%
This score indicates how likely this project is to be science-related based on various indicators:
-
○CITATION.cff file
-
✓codemeta.json file
Found codemeta.json file -
✓.zenodo.json file
Found .zenodo.json file -
○DOI references
-
○Academic publication links
-
○Committers with academic emails
-
○Institutional organization owner
-
○JOSS paper metadata
-
○Scientific vocabulary similarity
Low similarity (10.6%) to scientific vocabulary
Repository
Demo Swift REST
Basic Info
- Host: GitHub
- Owner: joelparkerhenderson
- Language: Swift
- Default Branch: master
- Size: 67.8 MB
Statistics
- Stars: 8
- Watchers: 6
- Forks: 2
- Open Issues: 0
- Releases: 0
Metadata Files
README.md
Demo Swift REST

This is a demonstration of:
The Swift programming language with Apple Xcode iOS
- How to create a simple REST app
- How to fetch data by using Alamofire
- How to parse JSON by using ObjectMapper
- How to save objects by using Realm
This README describes how to create the demo.
Start
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.
Create the project
Launch Xcode and create a new project.
Use the iOS template "Single View Application" and Project Name "Demo Swift REST".
Create a simple way to print some text to the screen.
We create a text view object and IBOutlet named "demoTextView".
Help.
Add Alamofire, ObjectMapper, and Realm. We suggest using Carthage or Cocoapods.
Carthage Cartfile:
github "Alamofire/Alamofire" github "Hearst-DD/ObjectMapper" ~> 2.2 github "realm/realm-cocoa"
If you want a simpler introduction to each piece of this demo, then see these related repos:
Add Alamofire
Edit ViewController.swift.
Add Alamofire networking code:
```swift 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 } } }
```
Verify Alamofire works by runing the app.
The Simulator screen shows the response result value string, which looks something like this.
json
{
"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
Create a directory named "Models".
Create a model called "Item" that implements the ObjectMappable interface:
```swift 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
Edit ViewController.swift.
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("https://httpbin.org/get")
.validate()
.responseString { response in
self.demoTextView.text = response.result.value
let item = Mapper<Item>().map(JSONString: response.result.value!)
self.demoTextView.text = item!.url!
}
}
}
Run the app.
The Simulator screen shows the item URL, which is "https://httpbin.org/get".
Upgrade the model to use Realm
Edit Models/Item.swift.
Add code to import RealmSwift, and add public init methods, and make the properties dynamic and default to nil.
```swift 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 public func mapping(map: Map) { origin <- map["origin"] url <- map["url"] }
} ```
Upgrade the view to use Realm
Edit ViewController.swift
Add code to open Realm, and write and item, and read an item.
```swift 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("https://httpbin.org/get")
.validate()
.responseString { response in
self.demoTextView.text = response.result.value
let item = Mapper<Item>().map(JSONString: response.result.value!)
self.demoTextView.text = item!.url!
// 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
}
}
```
Run
Run the app.
The Simulator screen shows the item URL, which is "https://httpbin.org/get".
Congratulations, you're successful!
Tracking
- Package: demoswiftrest
- Version: 3.0.0
- Created: 2016-05-30
- Updated: 2017-09-22
- License: BSD, GPL, MIT
- Contact: Joel Parker Henderson (http://joelparkerhenderson.com)
Owner
- Name: Joel Parker Henderson
- Login: joelparkerhenderson
- Kind: user
- Location: California
- Website: http://www.joelparkerhenderson.com
- Repositories: 319
- Profile: https://github.com/joelparkerhenderson
Software developer. Technology consultant. Creator of GitAlias.com, NumCommand.com, SixArm.com, and many open source projects.
GitHub Events
Total
- Push event: 1
Last Year
- Push event: 1
Committers
Last synced: about 1 year ago
Top Committers
| Name | Commits | |
|---|---|---|
| Joel Parker Henderson | j****l@j****m | 18 |
| joshmac | r****y@i****m | 3 |
Committer Domains (Top 20 + Academic)
Issues and Pull Requests
Last synced: about 1 year ago
All Time
- Total issues: 0
- Total pull requests: 2
- Average time to close issues: N/A
- Average time to close pull requests: about 4 hours
- Total issue authors: 0
- Total pull request authors: 1
- Average comments per issue: 0
- Average comments per pull request: 0.0
- Merged pull requests: 2
- Bot issues: 0
- Bot pull requests: 0
Past Year
- Issues: 0
- Pull requests: 0
- Average time to close issues: N/A
- Average time to close pull requests: N/A
- Issue authors: 0
- Pull request authors: 0
- Average comments per issue: 0
- Average comments per pull request: 0
- Merged pull requests: 0
- Bot issues: 0
- Bot pull requests: 0
Top Authors
Issue Authors
Pull Request Authors
- rockiesgrizzly (2)