Alamofire将CRUD分开

时间:2014-12-17 20:02:16

标签: alamofire

ViewController.swift 错误在调用Router.createSession中缺少参数#2的参数([" caio":" teste"])

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 Router

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
    }
}

}

我感谢你的帮助,我正在做一些需要这个实施的测试

1 个答案:

答案 0 :(得分:1)

确保在文件顶部正确import Alamofire。这为我解决了。