Created
October 6, 2017 16:11
-
-
Save vukcevich/f900b4d54918593a0a3861dce621449c to your computer and use it in GitHub Desktop.
Swift Enums as Result Type --
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
| //: Playground - noun: a place where people can play | |
| import PlaygroundSupport | |
| import UIKit | |
| import Foundation | |
| PlaygroundPage.current.needsIndefiniteExecution = true | |
| URLCache.shared = URLCache(memoryCapacity: 0, diskCapacity: 0, diskPath: nil) | |
| //https://mislavjavor.github.io/2017-04-19/Swift-enums-are-sum-types.-That-makes-them-very-interesting/?utm_content=buffer55854&utm_medium=social&utm_source=facebook.com&utm_campaign=buffer | |
| struct ProductExample { | |
| let a: Bool | |
| let b: Bool | |
| } | |
| let first = ProductExample(a: true, b: true) | |
| let second = ProductExample(a: true, b: false) | |
| let third = ProductExample(a: false, b: true) | |
| let fourth = ProductExample(a: false, b: false) | |
| struct ProductExampleTwo { | |
| let a: Bool | |
| let b: Int8 | |
| } | |
| /* | |
| Sum types (in swift) are enums! | |
| Number of possible values of a sum type is the sum of the number of possible values of it’s constituent parts | |
| */ | |
| enum SumExample { | |
| case a(Bool) | |
| case b(Bool) | |
| } | |
| let firstEnum = SumExample.a(true) | |
| let secondEnum = SumExample.b(true) | |
| let thirdEnum = SumExample.a(false) | |
| let fourthEnum = SumExample.b(false) | |
| enum SumExampleTwo { | |
| case a(Bool) | |
| case b(Int8) | |
| } | |
| //let someUrl:URL = URL(string:"http://www.yahoo.com/")! | |
| //I did not expect this to be valid url: | |
| //let someUrl:URL = URL(string:"http://www.yahoooooo.com/")! //to invoke optional error | |
| let someUrl:URL = URL(string:"http://www.yahoboo.com/")! //to error | |
| enum Result { | |
| case success(URLResponse) | |
| case error(Error) | |
| } | |
| typealias Handler = (Result) -> Void | |
| func getUser(from: URL, completionHandler: @escaping (Handler)) { | |
| // implementation | |
| // var request = URLRequest(url: URL(string: "http://example.com")!) | |
| var request = URLRequest(url: from) | |
| request.httpMethod = "POST" | |
| let session = URLSession.shared | |
| session.dataTask(with: request) {(data, response, err) in | |
| print("Entered the completionHandler") | |
| guard let _:Data = data as Data?, let _:URLResponse = response, err == nil else { | |
| print(#line, #function, "[dbg-err]: ", err!.localizedDescription) | |
| completionHandler(Result.error(err!)) | |
| return | |
| } | |
| //completionHandler(response as! Result) | |
| print("[data]: ", data!) | |
| print("[response]: ", response!) | |
| //Testing 'response' values, mainly header access: | |
| let httpResponse = response as! HTTPURLResponse | |
| let statusCode = httpResponse.statusCode | |
| //Note: Different Server response(HTTPURLResponse) will give different response values | |
| //So accordingly - allHeaderFields will vary depending on this response(HTTPURLResponse) | |
| print("\n") | |
| let vr = httpResponse.allHeaderFields["Content-Type"] | |
| print(#line, #function, " dbg-vr-]:", vr!) | |
| let vr0 = httpResponse.allHeaderFields["Content-Encoding"] | |
| print(#line, #function, "dbg-vr0]:", vr0!) | |
| let vr1 = httpResponse.allHeaderFields["Content-Length"] | |
| print(#line, #function, "dbg-vr1]:", vr1!) | |
| let vr2 = httpResponse.allHeaderFields["Cache-Control"] | |
| print(#line, #function, "dbg-vr2]:", vr2!) | |
| let vr3 = httpResponse.allHeaderFields["X-AspNetMvc-Version"] | |
| print(#line, #function, "dbg-vr3]:", vr3!) | |
| let vr4 = httpResponse.allHeaderFields["X-AspNet-Version"] | |
| print(#line, #function, "dbg-vr4]:", vr4!) | |
| let vr5 = httpResponse.allHeaderFields["X-Powered-By"] | |
| print(#line, #function, "dbg-vr5]:", vr5!) | |
| let vr6 = httpResponse.allHeaderFields["Server"] | |
| print(#line, #function, "dbg-vr6]:", vr6!) | |
| let vr7 = httpResponse.allHeaderFields["Vary"] | |
| print(#line, #function, "dbg-vr7]:", vr7!) | |
| let vr8 = httpResponse.allHeaderFields["Date"] | |
| print(#line, #function, "dbg-vr8]:", vr8!) | |
| print("\n") | |
| if (statusCode == 200) { | |
| print(#line, #function, "[dbg-statusCode]: ", statusCode) | |
| } | |
| completionHandler(Result.success( response!)) | |
| }.resume() | |
| // print(#line, #function, "[dbg-Handler]:", Handler.self) | |
| } | |
| //use case: | |
| getUser(from: someUrl) { (response) in | |
| // print(#line, #function, "dbg-response-101 ") | |
| print(#line, #function, "dbg-response]: ", response) | |
| switch response { | |
| case .success(let result): | |
| print(#line, #function, "[dbg-000]: ", result) | |
| let httpResponse = result as! HTTPURLResponse | |
| let statusCode = httpResponse.statusCode | |
| print(#line, #function, "[dbg-2-httpResponse.]: ", httpResponse.allHeaderFields) | |
| if (statusCode == 200) { | |
| print(#line, #function, "[dbg-2-statusCode]: ", statusCode) | |
| } | |
| case .error(let error): | |
| print(#line, #function, "[dbg-111]: " ,error.localizedDescription) | |
| } | |
| } | |
| //Enums syntax quick & dirty | |
| //Swift enums can be written like this: | |
| enum Foo1 { | |
| case a | |
| case b | |
| } | |
| let sth1 = Foo1.a | |
| let oth1 = Foo1.b | |
| //But can be also written like this: | |
| enum Foo { | |
| case a(String) | |
| case b(isEnabled: Bool) | |
| } | |
| let sth = Foo.a("Hello") | |
| let oth = Foo.b(isEnabled: false) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment