Swift 如何关闭 Facebook 图块
Swift how to close Facebook graph block
我想要,但我不知道如何 =(,关闭我的 Facebook 图块
我的问题是我想将我的用户 facebook 的用户名保存在一个变量中以便稍后使用它(另一个视图控制器),但我不能,因为我的块在我尝试保存数据后关闭了。
这是我的委托函数:
// Facebook Delegate Methods
func loginButton(connection: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) {
if (error != nil)
{
println("Error")
}
else {
println("User logged in")
self.returnUserData()
}
}
这是我使用 facebook 图的函数 api
// Here my block that I want to close, maybe with completion closure inside
func returnUserData()
{
let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email"])
// Launch asynchronous function
var userName : String = "error"
graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in
if ((error) != nil)
{
// Process error
println("Error: \(error)")
}
else
{
println(result)
userName = result["first_name"] as! String
}
})
}
有人可以帮我吗?
看看这个例子,你可以在你的闭包范围之外设置变量,然后在它的值被设置时要求它执行一个特定的函数,如下所示:
import UIKit
class ViewController: UIViewController {
var myString : String = "123" {
didSet {
self.stringUpdated()
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
myString = "456" // this would take place in your graph request closure
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func stringUpdated() {
println("got it, string is \(myString)")
}
}
我想要,但我不知道如何 =(,关闭我的 Facebook 图块
我的问题是我想将我的用户 facebook 的用户名保存在一个变量中以便稍后使用它(另一个视图控制器),但我不能,因为我的块在我尝试保存数据后关闭了。
这是我的委托函数:
// Facebook Delegate Methods
func loginButton(connection: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) {
if (error != nil)
{
println("Error")
}
else {
println("User logged in")
self.returnUserData()
}
}
这是我使用 facebook 图的函数 api
// Here my block that I want to close, maybe with completion closure inside
func returnUserData()
{
let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email"])
// Launch asynchronous function
var userName : String = "error"
graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in
if ((error) != nil)
{
// Process error
println("Error: \(error)")
}
else
{
println(result)
userName = result["first_name"] as! String
}
})
}
有人可以帮我吗?
看看这个例子,你可以在你的闭包范围之外设置变量,然后在它的值被设置时要求它执行一个特定的函数,如下所示:
import UIKit
class ViewController: UIViewController {
var myString : String = "123" {
didSet {
self.stringUpdated()
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
myString = "456" // this would take place in your graph request closure
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func stringUpdated() {
println("got it, string is \(myString)")
}
}