在闭包内部调用Swift闭包 [英] Calling Swift Closure Inside Closure

查看:205
本文介绍了在闭包内部调用Swift闭包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

  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 tried saying self inside the outer closure but it did not work. What am I missing?

已更新:仍然存在构建错误:

UPDATED: Still having build errors:

更新:如果我将getUserTimeline方法放在闭包之外,那么它将起作用.这一项工作.

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

但这不是:

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


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

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

    })



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


})

更新:getUserTimeLine方法的定义

UPDATE: Definition of getUserTimeLine method

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

更新:现在,我收到一个构建错误,提示缺少参数sinceID.我什至没有使用该构造函数.

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

            })

        }

推荐答案

尝试:

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

在这种情况下

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

是具有隐式非Void返回值的单表达式闭包".

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

从Xcode 6.2/Swift 1.1开始,您需要在此处显式return.

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

或者,使用已解决此问题的Xcode 6.3/Swift 1.2.

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

请参阅以下问题:单行闭包而没有返回类型 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆