1
votes

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?

1
Have you tried removing the explicit type from it? success:{ objects in /* ... */ }. And please post your code and the exact error message, not a screenshot of both. - luk2302
Please update your question and post your code as text. It makes it much easier to read and reference. - rmaddy

1 Answers

2
votes

The function you're using require a closure that accept as a single argument a Swift array, an implicitly unwrapped one, of AnyObjects ([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 AnyObjects 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.