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

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

定义

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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}"
}
}
}
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}" } } }
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}"
        }
    }
}

用法:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
func url(route: Moya) -> NSURL {
return route.baseURL.URLByAppendingPathComponent(route.path)
}
func url(route: Moya) -> NSURL { return route.baseURL.URLByAppendingPathComponent(route.path) }
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 *