我正在编写一个Router类来驱动我的API(CRUD),如Alamofire文档中所规定的那样,令人讨厌的是,我最终反复重复相同的代码行。这是枚举的一部分:
enum Router: URLRequestConvertible {
static let baseURLString = "http://xxx.xxx.xxx.xxx"
case ReadFeed([String: AnyObject])
case CreateUser([String: AnyObject])
case LogIn([String:AnyObject])
case CreatePost([String:AnyObject])
case ReadUser(id: String, parameters: [String:AnyObject])
case ListUsers([String:AnyObject])
case UpdateUser([String: AnyObject])
case DestroyUser(String)
case ReadImage(String)
case Subscribe(String, to: String)
var method: Alamofire.Method {
switch self {
case .CreateUser, .CreatePost:
return .POST
case .ReadUser, .ListUsers, .ReadImage, .ReadFeed, .SearchUser:
return .GET
case .UpdateUser:
return .PUT
case .DestroyUser:
return .DELETE
case .LogIn:
return .POST
}
}
var path: String {
switch self {
case .ReadFeed:
return "/feed"
case .CreateUser:
return "/users"
case .CreateColumn:
return "/columns"
case .UploadImage:
return "/profilepictures"
case .ListUsers:
return "/users"
case .ReadUser(let id, _):
return "/users/\(id)"
case .CreatePost:
return "/posts"
case .LogIn:
return "/users/login"
case .ReadImage(let ownerID):
return "/image/\(ownerID)"
case .Subscribe(let user, _):
return "/users/\(user)"
case .UpdateUser(let username, _):
return "/users/\(username)"
case .DestroyUser(let username):
return "/users/\(username)"
}
}
// MARK: URLRequestConvertible
var URLRequest: NSMutableURLRequest {
let URL = NSURL(string: Router.baseURLString)!
let mutableURLRequest = NSMutableURLRequest(URL: URL.URLByAppendingPathComponent(path))
mutableURLRequest.HTTPMethod = method.rawValue
switch self {
case .CreateUser(let parameters):
return Alamofire.ParameterEncoding.JSON.encode(mutableURLRequest, parameters: parameters).0
return Alamofire.ParameterEncoding.JSON.encode(mutableURLRequest, parameters: parameters).0
case .CreatePost(let parameters):
return Alamofire.ParameterEncoding.JSON.encode(mutableURLRequest, parameters: parameters).0
case .UploadImage( _, let image, let parameters):
mutableURLRequest.HTTPBody = UIImageJPEGRepresentation(image, 1)
return Alamofire.ParameterEncoding.URL.encode(mutableURLRequest, parameters: parameters).0
case .UpdateUser(_, let parameters):
return Alamofire.ParameterEncoding.URL.encode(mutableURLRequest, parameters: parameters).0
case .ListUsers(let parameters):
return Alamofire.ParameterEncoding.URL.encode(mutableURLRequest, parameters: parameters).0
case .ReadFeed(let parameters):
return Alamofire.ParameterEncoding.URL.encode(mutableURLRequest, parameters: parameters).0
case .ReadUser(_, let parameters):
return Alamofire.ParameterEncoding.URL.encode(mutableURLRequest, parameters: parameters).0
case .LogIn(let parameters):
return Alamofire.ParameterEncoding.URL.encode(mutableURLRequest, parameters: parameters).0
case .Subscribe(_, let newSubscription):
let parameters: [String:AnyObject] = ["subscriptions": ["$push": newSubscription]]
return Alamofire.ParameterEncoding.JSON.encode(mutableURLRequest, parameters: parameters).0
default:
return mutableURLRequest
}
}
}
如您所见,公式
case .XXX(let parameters):
return Alamofire.ParameterEncoding.URL.encode(mutableURLRequest, parameters: parameters).0
重复次数和次数。有没有办法避免这种情况?
答案 0 :(得分:1)
在所有情况下,您似乎都有parameters
,因此可能不应与每个案例相关联。类似于此的结构可能很有用:
struct RouterInfo {
let type: Router
let parameters: [String:AnyObject]
}
然后只有enum Router
中的某些案例可能会有一些特定于这些案例的关联值。
然后,您的var URLRequest
应该是结构的一部分。
答案 1 :(得分:0)
你可以定义更多名为'parameters'的变量,它是[String:AnyObject]的一种类型吗?并为每个案例返回这些参数。
请注意,.get服务需要参数为nil,如果.post服务没有参数,则返回[:](空字典)
var parameters: [String: AnyObject]? {
switch self {
case .login(let username, let password):
return [
"username": username,
"password": password
]
case .getUserInfo(let userID):
return ["userID": userID]
default:
return nil
}
}
然后在mutableURLRequest中调用此变量
var URLRequest: NSMutableURLRequest {
let URL = NSURL(string: Router.baseURLString)!
let mutableURLRequest = NSMutableURLRequest(URL: URL.URLByAppendingPathComponent(path))
mutableURLRequest.HTTPMethod = method.rawValue
// Get your parameters via calling the variable
return Alamofire.ParameterEncoding.JSON.encode(mutableURLRequest, parameters: parameters).0
}
答案 2 :(得分:0)
我认为你可以做这样的事情
switch self {
case .CreateUser(let parameters):
return Alamofire.ParameterEncoding.JSON.encode(mutableURLRequest, parameters: parameters).0
return Alamofire.ParameterEncoding.JSON.encode(mutableURLRequest, parameters: parameters).0
case .CreatePost(let parameters):
return Alamofire.ParameterEncoding.JSON.encode(mutableURLRequest, parameters: parameters).0
case .UploadImage( _, let image, let parameters):
mutableURLRequest.HTTPBody = UIImageJPEGRepresentation(image, 1)
return Alamofire.ParameterEncoding.URL.encode(mutableURLRequest, parameters: parameters).0
case .UpdateUser(_, let parameters), .ListUsers(let parameters), .ReadFeed(let parameters), .ReadUser(_, let parameters), case .LogIn(let parameters):
return Alamofire.ParameterEncoding.URL.encode(mutableURLRequest, parameters: parameters).0
case .Subscribe(_, let newSubscription):
let parameters: [String:AnyObject] = ["subscriptions": ["$push": newSubscription]]
return Alamofire.ParameterEncoding.JSON.encode(mutableURLRequest, parameters: parameters).0
default:
return mutableURLRequest
}
通过这种方式,您将获得最少的重复次数。