In the view controller I have this code:
newPost!.save({ (post, error) in
//Error here.
if let anError = error {
print("error calling POST on /posts")
print(anError)
return
}
guard let post = post else {
print("error calling POST on /posts: result is nil")
return
}
// success!
print(post.description())
print(post.title)
})
I don't quite understand the Result<Whatever>
syntax, Result
should be something from Alamofire 2, Post
is a class I created, but what does Result<Post> -> Void
altogether mean and what should I change about it?
Update:
func save(completionHandler: (Result<Post>) -> Void) {
let fields: [String: AnyObject]? = self.toJSON()
if fields == nil {
print("Error: error converting newPost fields to JSON")
return
}
Your completion handler definition and the way you have called it are different, try something like this for your completion handler if you want to call it that way:
func save(completionHandler: (post:Post?, error:String?) -> Void) {
let fields: [String: AnyObject]? = self.toJSON()
if fields == nil {
completionHandler(nil, "Error converting newPost fields to JSON")
return
}
// rest of code, eventually call completionHandler with success
}
For Swift 2:
func save(completionHandler: (post:Post?, error:String?) -> Void) {
guard let fields = self.toJSON() else {
completionHandler(nil, "Error converting newPost fields to JSON")
return
}
// rest of code, eventually call completionHandler with success
}