var first_name = ""
func problemFunc() {
FBRequestConnection.startForMeWithCompletionHandler { (connection: FBRequestConnection!, result: AnyObject!, error: NSError!) -> Void in
if let fbGraphUserDict = result as? Dictionary<String, AnyObject>{
first_name = fbGraphUserDict["first_name"] as NSString
println(first_name)
}
}
}
PFFacebookUtils.logInWithPermissions(permissions, {
(user: PFUser!, error: NSError!) -> Void in
if user == nil {
NSLog("Uh oh. The user cancelled the Facebook login.")
} else if user.isNew {
NSLog("User signed up and logged in through Facebook!")
} else {
NSLog("User logged in through Facebook!")
problemFunc() // error is here
}
})
Run Code Online (Sandbox Code Playgroud)
此代码位于@Ibaction按钮内.我无法构建,因为对problemFunc()的调用会触发此帖子标题中的错误消息.如果我在problemFunc中移动first_name var定义,它将正常工作.但是我需要它,因为另一个函数需要访问它的值.我真的不确定导致这个问题的原因,如果你有线索,请帮忙.
flu*_*nic 27
使用闭包而不是函数:
var first_name = ""
let problemFunc = { () -> () in
FBRequestConnection.startForMeWithCompletionHandler { (connection: FBRequestConnection!, result: AnyObject!, error: NSError!) -> Void in
if let fbGraphUserDict = result as? Dictionary<String, AnyObject>{
first_name = fbGraphUserDict["first_name"] as NSString
println(first_name)
}
}
}
PFFacebookUtils.logInWithPermissions(permissions, {
(user: PFUser!, error: NSError!) -> Void in
if user == nil {
NSLog("Uh oh. The user cancelled the Facebook login.")
} else if user.isNew {
NSLog("User signed up and logged in through Facebook!")
} else {
NSLog("User logged in through Facebook!")
problemFunc() // error is here
}
})
Run Code Online (Sandbox Code Playgroud)
erp*_*ker 12
以下是基本原则:(来自Apple的文档:https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html#//apple_ref/doc/uid/TP40014097-CH11 -ID103)
"函数中引入的全局函数和嵌套函数实际上是闭包的特例.闭包采用以下三种形式之一:
即没关系
func someFunc() {
func nestFunc() {}
}
Run Code Online (Sandbox Code Playgroud)
但事实并非如此
func someFunc() {
func nestFunc() {
func nestedFunc2() { }
}
}
Run Code Online (Sandbox Code Playgroud)
如果你在Xcode中看到这个,第三个函数(func nestedFunc2)会给你错误"无法通过从另一个本地函数捕获来引用本地函数"
top函数(func someFunc)是一个全局范围函数,它们像常规函数/方法一样工作.
第二个函数(func nestFunc)是一个嵌套函数,它是一个深度的命名闭包,可以捕获其父全局函数的范围.
嵌套函数可以捕获全局函数的范围,但不能捕获另一个嵌套函数的范围.
这就是为什么我们需要一个闭包即
func someFunc() {
func nestFunc() {
let strictClosure = { () -> () in
//this is where you write the code
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2461 次 |
| 最近记录: |