swift 中一种不错的url拼接实战

转自自:http://chris.eidhof.nl/post/typesafe-url-routes-in-swift/

定义

extension Github : Moya {
    var baseURL: NSURL { return NSURL(string: "https://api.github.com") }
    var sampleData: String {
        switch self {
        case .Zen: return "Half measures are as bad as nothing at all."
        case .UserProfile(let name): return "{login: \"\(name)\", id: 100}"
        }
    }
}

用法:

func url(route: Moya) -> NSURL {
    return route.baseURL.URLByAppendingPathComponent(route.path)
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *