This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var request = URLRequest(url:URL(string:"http://localhost:8888/login")!) | |
| request.httpMethod = "POST" | |
| let params = ["email":"[email protected]", "password":"password"] | |
| request.httpBody = try? JSONSerialization.data(withJSONObject: params, options: []) | |
| request.addValue("application/json", forHTTPHeaderField: "Content-Type") | |
| URLSession.shared.dataTask(with: request) { (data:Data?, response:URLResponse?, error:Error?) in | |
| if let safeData = data{ | |
| print("response: \(String(data:safeData, encoding:.utf8))") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Nikka | |
| //Define your provider | |
| class MyProvider:HTTPProvider { | |
| var baseURL = URL(string:"https://my-website.com/api")! | |
| } | |
| //Define your API endpoints | |
| extension Route { | |
| static let me = Route(path:"/me", method: .get} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| struct User:Mappable{ | |
| let id:Int | |
| let name:String | |
| init(map: Mapper) throws { | |
| self.id = try map.from("id") | |
| self.name = try map.from("name") | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let userFuture:Future<User> = myProvider.request(.login(email, password)).responseObject() | |
| let feedFuture:Future<Feed> = userFuture.flatMap {self.myProvider.request(.feed($0.id)).responseObject()} | |
| feedFuture.onComplete { (result:Result<Feed>) in | |
| switch result{ | |
| case .success(let feed): | |
| print("logged in successfully, and feed retrieved") | |
| case .failure(let error): | |
| print("an error occurred on the way") | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let facebookTokenObs:Observable<String> = FBSDKLoginManager().logIn(with: ["email"], from: self) | |
| let userObs:Observable<User> = facebookTokenObs.flatMap {self.myProvider.request(.me($0)).responseObject()} | |
| userObs.subscribe(onNext: { (user:User) in | |
| //Do something with the user | |
| }, onError:{ (error:Error) in | |
| //Display an error message | |
| }).addDisposableTo(disposeBag) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Nikka | |
| struct MyProvider:HTTPProvider { | |
| let baseURL = URL(string:"https://my-website.com/api")! | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //User endpoints | |
| extension Route { | |
| static let me = Route(path:"/me", method:.get) | |
| static let friends = Route(path:"/me/friends") //Default method used is GET | |
| static let login = {(email:String, password:String) in //You can define a Route as a closure that takes parameters | |
| Route(path:"/login", method:.post, params:["email":email, "password":password])} | |
| static let user = {(id:String) in Route(path:"/user/\(id)")} | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let myProvider = MyProvider() | |
| //This will send a POST request to the endpoint https://my-website.com/api/login with a json body `{"email":"[email protected]","password":"bar"}`` | |
| myProvider.request(.login("[email protected]", "password")).responseJSON { (response:Response<Any>) in | |
| //Parse here the object as an array or dictionary | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Person { | |
| let name: String | |
| var apartment: Apartment? | |
| init(name: String) { self.name = name } | |
| } | |
| class Apartment { | |
| let unit: String | |
| var tenant: Person? | |
| init(unit: String) { self.unit = unit } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class CustomCell: UITableViewCell { | |
| @IBOutlet weak var customButton: UIButton! | |
| var onButtonTap:(()->Void)? | |
| @IBAction func buttonTap(){ | |
| onButtonTap?() | |
| } | |
| } |
OlderNewer