Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 无法引用从另一个本地函数(swift)捕获的本地函数 var first_name=“” func problemFunc(){ FBRequestConnection.StartFormWithCompletionHandler{(连接:FBRequestConnection!,结果:AnyObject!,错误:NSError!)->中的Void 如果让fbGraphUserDict=结果为字典{ first_name=fbGraphUserDict[“first_name”]作为NSString println(名字) } } } PFFacebookUtils.logInWithPermissions(权限{ (用户:PFUser!,错误:NSError!)->在中无效 如果用户==nil{ NSLog(“哦,用户取消了Facebook登录。”) }否则,如果user.isNew{ NSLog(“用户注册并通过Facebook登录!”) }否则{ NSLog(“用户通过Facebook登录!”) problemFunc()//此处有错误 } })_Ios_Swift - Fatal编程技术网

Ios 无法引用从另一个本地函数(swift)捕获的本地函数 var first_name=“” func problemFunc(){ FBRequestConnection.StartFormWithCompletionHandler{(连接:FBRequestConnection!,结果:AnyObject!,错误:NSError!)->中的Void 如果让fbGraphUserDict=结果为字典{ first_name=fbGraphUserDict[“first_name”]作为NSString println(名字) } } } PFFacebookUtils.logInWithPermissions(权限{ (用户:PFUser!,错误:NSError!)->在中无效 如果用户==nil{ NSLog(“哦,用户取消了Facebook登录。”) }否则,如果user.isNew{ NSLog(“用户注册并通过Facebook登录!”) }否则{ NSLog(“用户通过Facebook登录!”) problemFunc()//此处有错误 } })

Ios 无法引用从另一个本地函数(swift)捕获的本地函数 var first_name=“” func problemFunc(){ FBRequestConnection.StartFormWithCompletionHandler{(连接:FBRequestConnection!,结果:AnyObject!,错误:NSError!)->中的Void 如果让fbGraphUserDict=结果为字典{ first_name=fbGraphUserDict[“first_name”]作为NSString println(名字) } } } PFFacebookUtils.logInWithPermissions(权限{ (用户:PFUser!,错误:NSError!)->在中无效 如果用户==nil{ NSLog(“哦,用户取消了Facebook登录。”) }否则,如果user.isNew{ NSLog(“用户注册并通过Facebook登录!”) }否则{ NSLog(“用户通过Facebook登录!”) problemFunc()//此处有错误 } }),ios,swift,Ios,Swift,此代码位于@iAction按钮内。我无法生成,因为调用problemFunc()会触发此文章标题中的错误消息。如果我在problemFunc中移动first_name var定义,它将正常工作。但我需要它,因为另一个函数需要访问它的值。 我真的不知道是什么导致了这个问题,如果您有线索,请帮助。使用闭包而不是函数: var first_name = "" func problemFunc() { FBRequestConnection.startForMeWithCo

此代码位于@iAction按钮内。我无法生成,因为调用problemFunc()会触发此文章标题中的错误消息。如果我在problemFunc中移动first_name var定义,它将正常工作。但我需要它,因为另一个函数需要访问它的值。
我真的不知道是什么导致了这个问题,如果您有线索,请帮助。

使用闭包而不是函数:

 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

        }
    })
var first_name=“”
让problemFunc={()->()进入
FBRequestConnection.StartFormWithCompletionHandler{(连接:FBRequestConnection!,结果:AnyObject!,错误:NSError!)->中的Void
如果让fbGraphUserDict=结果为字典{
first_name=fbGraphUserDict[“first_name”]作为NSString
println(名字)
}
}
}
PFFacebookUtils.logInWithPermissions(权限{
(用户:PFUser!,错误:NSError!)->在中无效
如果用户==nil{
NSLog(“哦,用户取消了Facebook登录。”)
}否则,如果user.isNew{
NSLog(“用户注册并通过Facebook登录!”)
}否则{
NSLog(“用户通过Facebook登录!”)
problemFunc()//此处有错误
}
})

@fluidsonic answer应该可以解决这个问题。但是请注意,您正在执行一些意大利面代码,因为您正在修改闭包捕获的变量,并在另一个函数的上下文中执行。如果需要调试,则很难跟踪,更一般地说,很难跟踪何时以及如何修改该变量

一个更线性、可读性更好的流程是将
problemFunc
定义为一个以函数为参数的函数,并调用该函数,而不是直接在
first\u name
变量中设置值:

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

    }
})

以下是游戏中的基本原则:(来自苹果的文档:)

“函数中介绍的全局函数和嵌套函数实际上是闭包的特殊情况。闭包有三种形式之一:

  • 全局函数是具有名称且不捕获任何值的闭包
  • 嵌套函数是具有名称的闭包,可以从其封闭函数中捕获值
  • 闭包表达式是以轻量级语法编写的未命名闭包,可以从其周围上下文捕获值。”
这没关系

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 { (name: String) -> Void in
            first_name = name
        }
    }
})
但事实并非如此

func someFunc() {
  func nestFunc()  {}
}
如果您在Xcode中看到这一点,第三个函数(func nestedFunc2)将显示错误“无法引用从另一个本地函数捕获的本地函数”

top函数(func someFunc)是一个全局作用域函数,其工作原理与常规函数/方法类似

第二个函数(func nestFunc)是一个嵌套函数,它是一个一级深的命名闭包,可以捕获其父全局函数的范围

嵌套函数,可以捕获全局函数的范围,但不能捕获另一个嵌套函数的范围

这就是为什么我们需要结束,即


谢谢,很有效,但出于某种原因我不得不删除:->()非常感谢!避免这件丑陋的事情是个好主意+一路上都有精彩的解释。我不知道发生了什么,但你让我很容易理解+1.
func someFunc() {
  func nestFunc()  {}
}
func someFunc() {
  func nestFunc()  {
     func nestedFunc2() { }
  }
}
func someFunc() {
   func nestFunc()  {
       let strictClosure = { () -> () in
        //this is where you write the code
        }
   }
}