SO originally I was having some trouble with Swift 2 closures, here was my problem:
func getImgurHotListWithViralBool(viral:Bool) -> NSArray
{
IMGGalleryRequest.hotGalleryPage(0, withViralSort: viral,
success:{
(objects:NSArray) in
//It gives the error here*********
},
failure: {(error:NSError) in
})
}
It gives the error:
Cannot convert value of type '(NSArray) -> ()' to expected argument type '(([AnyObject]!) -> Void)!'
UPDATE: Thankfully, Marco Boschi helped me with this solution;
func getImgurHotListWithViralBool(viral:Bool) -> NSArray {
IMGGalleryRequest.hotGalleryPage(0, withViralSort: viral,
success: { (objects: [AnyObject]!) in
// ...
}, failure: { (error:NSError) in
// ...
})
}
And now the error is present in error:NSError
i.e :
Cannot convert value of type '(NSError) -> ()' to expected argument type '(([AnyObject]!) -> Void)!'
What should I do?
The function you're using require a closure that accept as a single argument a Swift array, an implicitly unwrapped one, of AnyObject
s ([AnyObject]!
) as stated in the error message, but you are using an old NSArray
and the compiler cannot convert the type of your closure to the requested one, hence the error, changing the code as below will solve it.
func getImgurHotListWithViralBool(viral:Bool) -> NSArray {
IMGGalleryRequest.hotGalleryPage(0, withViralSort: viral,
success: { (objects: [AnyObject]!) in
// ...
}, failure: { (error:NSError) in
// ...
})
}
UPDATE: the second error you get is the same as before, the API wants a closure that accept an implicitly unwrapped array of AnyObject
s but you provide one that take an NSError
, you'll have to change the signature of failure
to
failure: { (error: [AnyObject]!) in
// ...
}
in order to solve it. Make sure to check the documentation of your API to know how to get the error from the array.