Parse / Swift初学者:如何在Parse查询上更改标签

时间:2015-06-06 01:46:36

标签: ios swift parse-platform

我正在创建一个帐户页面,需要更改我的IBOutlet"标题"成为用户" firstName"的第一个名字" _用户"类。我似乎无法使objectId部分工作,因为它显然需要为每个用户进行更改。无论如何,这是我正在做的事情:

import Parse
import UIKit
import ParseUI
import Bolts

@IBOutlet weak var header: UILabel!
var user = PFObject(className: "_User")

override func viewDidLoad() {
    super.viewDidLoad()
    var query = PFQuery(className: "_User")
    query.getObjectInBackgroundWithId(user.objectId) {
        (nameAgain: PFObject!, error: NSError!) -> Void in
        if error == nil {
            header.text = nameAgain.objectForKey("firstName")! as! String
        } else {
            println("Error")
        }
    }
}

提前致谢!

2 个答案:

答案 0 :(得分:0)

根据您的代码,很难理解您希望如何获取用户名称:

    var user = PFObject(className: "_User") //You create a var user of PFObject
    override func viewDidLoad() {
        super.viewDidLoad()
        var query = PFQuery(className: "_User")
        query.getObjectInBackgroundWithId(user.objectId) {  //Now you are trying to use, at this point you have no users in you var user
            (nameAgain: PFObject!, error: NSError!) -> Void in
            if error == nil { 
                header.text = nameAgain.objectForKey("firstName")! as! String
            } else {
                println("Error")
            }
        }
    }

我将在这里添加一些建议:

此函数查询Parse

中Users表中的所有用户
func userQuery(){
    var query = PFUser.query()
    query?.findObjectsInBackgroundWithBlock({
        (objects:[AnyObject]?, error:NSError?) -> Void in
        for object in objects!{
            let user = object as! PFUser
            println(user.username!)
        }
    })
}

此登录特定用户:

func loginUser(){
    PFUser.logInWithUsernameInBackground("User1", password: "Password1") {
        (user:PFUser?, error:NSError?) -> Void in
        if (error == nil){
            println("user login")
        } else {
            println("error login user - \(error?.localizedDescription)")
        }
    }
}

这会创建一个新用户:

func createUser(){
    var user = PFUser()
    user.username = "Test5"
    user.password = "Test5"
    user.signUpInBackgroundWithBlock {
        (sucess: Bool, error: NSError?) -> Void in
        if (error == nil){
            println("user created")
        } else {
            println("error creating user - \(error?.localizedDescription)")
        }
    }
}

我希望能帮到你!

答案 1 :(得分:0)

如果你想要的只是获取当前登录用户的名字,你可以使用

if let firstName = PFUser.currentUser()?["firstName"] as? String {
    header.text=firstName
}

PFUser.currentUser()返回PFUser对象(PFOBject的子类)。然后,您可以通过列简单地索引以获取值