Alamofire 用于 CRUD 的单独类
Posted
技术标签:
【中文标题】Alamofire 用于 CRUD 的单独类【英文标题】:Alamofire separate classes for CRUD 【发布时间】:2014-12-17 20:02:15 【问题描述】:ViewController.swift 调用 Router.createSession(["caio":"teste"]) 中参数 #2 的错误缺少参数
func loginViewFetchedUserInfo(loginView : FBLoginView!, user: FBGraphUser)
println("User: \(user)")
println("User ID: \(user.objectID)")
println("User Name: \(user.name)")
var userEmail = user.objectForKey("email") as String
println("User Email: \(userEmail)")
Alamofire.request(Router.CreateSession(["caio":"teste"])).response (request, response, data, error) in
println(request)
println(response)
println(error)
Router.swift 文件包含 crud 路由器
enum Router: URLRequestConvertible
static let baseURLString = "http://www.teste.com.br"
static var OAuthToken: String?
case CreateSession([String: AnyObject])
case ReadSession(String)
case UpdateSession(String, [String: AnyObject])
case DestroySession(String)
var method: Alamofire.Method
switch self
case .CreateSession:
return .POST
case .ReadSession:
return .GET
case .UpdateSession:
return .PUT
case .DestroySession:
return .DELETE
var path: String
switch self
case .CreateSession:
return "/sessions"
case .ReadSession(let username):
return "/sessions/\(username)"
case .UpdateSession(let username, _):
return "/sessions/\(username)"
case .DestroySession(let username):
return "/sessions/\(username)"
var URLRequest: NSURLRequest
let URL = NSURL(string: Router.baseURLString)!
let mutableURLRequest = NSMutableURLRequest(URL: URL.URLByAppendingPathComponent(path))
mutableURLRequest.HTTPMethod = method.rawValue
if let token = Router.OAuthToken
mutableURLRequest.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
switch self
case .CreateSession(let parameters):
return Alamofire.ParameterEncoding.JSON.encode(mutableURLRequest, parameters: parameters).0
case .UpdateSession(_, let parameters):
return Alamofire.ParameterEncoding.URL.encode(mutableURLRequest, parameters: parameters).0
default:
return mutableURLRequest
感谢您的帮助,我正在做测试,需要这个 crud 来实现
【问题讨论】:
【参考方案1】:确保文件顶部的import Alamofire
正确无误。这为我解决了问题。
【讨论】:
以上是关于Alamofire 用于 CRUD 的单独类的主要内容,如果未能解决你的问题,请参考以下文章
使用 Alamofire 将文件保存到 .DocumentDirectory
哪个是 xcode 7.2 支持的 alamofire 对象映射器和 alamofire pod 版本