Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling Swift Closure Inside Closure

Tags:

ios

swift

I have the following code:

  twitterAPI?.verifyCredentialsWithUserSuccessBlock({ (userName, password) -> Void in


            twitterAPI?.getUserTimelineWithScreenName(userName, count: 100, successBlock: { ([AnyObject]!) -> Void in



                }, errorBlock: { (error :NSError!) -> Void in

            })



            }, errorBlock: { (error :NSError!) -> Void in

                println("error block")
        })

I am getting the following errors:

enter image description here

I tried saying self inside the outer closure but it did not work. What am I missing?

UPDATED: Still having build errors:

enter image description here

UPDATE: If I put the getUserTimeline method outside the closure then it works. THIS ONE WORKS.

//        twitterAPI?.getUserTimelineWithScreenName("", successBlock: { (objects :[AnyObject]!) -> Void in
//            
//            }, errorBlock: { (error: NSError!) -> Void in
//        
//        })

But this DOES NOT:

twitterAPI?.verifyCredentialsWithUserSuccessBlock({ (userName, password) -> Void in


    self.twitterAPI?.getUserTimelineWithScreenName("", successBlock: { (objects :[AnyObject]!) -> Void in

        }, errorBlock: { (error: NSError!) -> Void in

    })



    }, errorBlock: { (error :NSError!) -> Void in


})

UPDATE: Definition of getUserTimeLine method

self.twitterAPI?.getUserTimelineWithScreenName(<#screenName: String!#>, successBlock: <#(([AnyObject]!) -> Void)!##([AnyObject]!) -> Void#>, errorBlock: <#((NSError!) -> Void)!##(NSError!) -> Void#>)

enter image description here

UPDATE: Now, I am getting a build error saying missing argument sinceID. I am not even using that constructor.

 if let twitterAPI = self.twitterAPI {

            twitterAPI.verifyCredentialsWithUserSuccessBlock({ (userName, password) -> Void in

                twitterAPI.getUserTimelineWithScreenName(userName, successBlock: { (objects :[AnyObject]!) -> Void in

                    }, errorBlock: { (error :NSError!) -> Void in

                })


                }, errorBlock: { (error :NSError!) -> Void in

            })

        }
like image 809
john doe Avatar asked Apr 07 '15 19:04

john doe


2 Answers

Try:

        twitterAPI?.verifyCredentialsWithUserSuccessBlock({ (userName, password) -> Void in
            self.twitterAPI?.getUserTimelineWithScreenName(userName, successBlock: { (objects :[AnyObject]!) -> Void in

                }, errorBlock: { (error :NSError!) -> Void in
            })

            return  // <-- ADDED

            }, errorBlock: { (error :NSError!) -> Void in
        })

In this case

{ (userName, password) -> Void in
    self.twitterAPI?.getUserTimelineWithScreenName("", successBlock: { (objects :[AnyObject]!) -> Void in
    }, errorBlock: { (error: NSError!) -> Void in
    })
}

is a "single expression closure" that has implicit non Void return.

As of Xcode 6.2 / Swift 1.1, you need explicit return here.

Or, use Xcode 6.3 / Swift 1.2 that has fixed this problem.

See this question: One-line closure without return type or Swift - 'Bool' is not a subtype of 'Void'?

like image 73
rintaro Avatar answered Oct 19 '22 23:10

rintaro


Ok, by the method names you are using, I'm guessing you are using the STTwitter library. If that's the case, you'll want something like this:

    if let twitterAPI = self.twitterAPI {
        twitterAPI.verifyCredentialsWithSuccessBlock({ (String) -> Void in
            twitterAPI.getUserTimelineWithScreenName("test", successBlock: { (objects: [AnyObject]!) -> Void in
                println("success")
                }, errorBlock: { (error: NSError!) -> Void in
                    println("failure")
            })
            }, errorBlock: { (error: NSError!) -> Void in

        })
    }

Note the let call before using the optional self.twitterAPI variable.

like image 41
danielbeard Avatar answered Oct 20 '22 01:10

danielbeard