swift Swift 3使用这篇精彩文章中的简单示例更新了代码:http://alisoftware.github.io/swift/async/error/2016/02/06/async-err

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift Swift 3使用这篇精彩文章中的简单示例更新了代码:http://alisoftware.github.io/swift/async/error/2016/02/06/async-err相关的知识,希望对你有一定的参考价值。

// A Gist.
struct Gist {
    let id: String
}

typealias JSONDictionary = [String: AnyObject]

// These methods are "helpers" to transform Data -> JSON -> Result<[Gist]> 
func jsonFrom(data: Data) -> Result<AnyObject> {
    let json = try! JSONSerialization.jsonObject(with: data, options: [])
    return Result.success(json as AnyObject)
}

func gistFrom(json: JSONDictionary) -> Gist {
    let id = json["id"] as! String
    return Gist(id: id)
}

func gistsFrom(json: AnyObject) -> Result<[Gist]> {
    let gistsJSON = json as! [JSONDictionary]
    let gists = gistsJSON.flatMap { gistFrom(json: $0) }
    return Result.success(gists)
}

// Result Type w/ extensions
enum Result<T> {
    case success(T)
    case failure(Error)
}

extension Result {
    func map<U>(f: (T) -> U) -> Result<U> {
        switch self {
        case .success(let t): return .success(f(t))
        case .failure(let error): return .failure(error)
        }
    }
    
    func flatMap<U>(f: (T) -> Result<U>) -> Result<U> {
        switch self {
        case .success(let t): return f(t)
        case .failure(let error): return .failure(error)
        }
    }
}

extension Result {
    // Return the value if it is a .success or throw the error if it is a .failure
    func resolve() throws -> T {
        switch self {
        case Result.success(let value): return value
        case Result.failure(let error): throw error
        }
    }
    
    // Construct a .success if the expression returns a value or a .failure if it throws
    init( _ throwingExpression: (Void) throws -> T) {
        do {
            let value = try throwingExpression()
            self = Result.success(value)
        } catch {
            self = Result.failure(error)
        }
    }
}

// Error Type
enum GistError: Error {
    case badData(Data)
}

let url = URL(string: "https://api.github.com/gists/public")!

fetch(url: url) { (resultData: Result<Data>) in
    let resultGists = resultData
        .flatMap(f: jsonFrom)
        .flatMap(f: gistsFrom)
    
    // Then if we want to go back in the do/try/catch world for the rest of the code:
    do {
        let gists = try resultGists.resolve()
    } catch {
        print("error")
    }
}

以上是关于swift Swift 3使用这篇精彩文章中的简单示例更新了代码:http://alisoftware.github.io/swift/async/error/2016/02/06/async-err的主要内容,如果未能解决你的问题,请参考以下文章

苹果 swift 3 中的网页抓取

Swift之字符串(String)

Swift Snapkit 看这篇基本就"够"了

Swift 使用CollectionView 实现图片轮播封装就是这样简单

Swift 3 中的新特性

swift3.0 图片放大缩小动画效果