如何从parse.com获取数据并在文本字段中设置它们?

时间:2015-08-02 02:42:51

标签: ios swift parse-platform

[Swift]编码器

有人可以解释我如何从parse.com获取数据以在文本字段中设置它们?我做了一个查询,但没有工作:

    if($row = mysqli_fetch_assoc($query)) {

    }

我正在尝试获取当前用户“用户名”并在文本字段中显示,我正在等待帮助!

1 个答案:

答案 0 :(得分:0)

textfield.text = usernamearray[0]

您的代码似乎使用了您要返回的用户名。你可以使用:

if let currentUser = PFUser.currentUser() {

    textfield.text = currentUser.username
}

<强>更新

var currentuser = PFUser.currentUser().username
var query = PFQuery(className: "_User")
query.whereKey("username", equalTo: currentuser)

query.findObjectsInBackgroundWithBlock {

    (objects: [AnyObject]!, error:NSError?) -> Void in

        if error == nil {

            for object in objects {

            println(object) //only for debugging 

                self.usernamearray.append(object["username"] as String!)
                textfield.text = usernamearray[0]
             }
         }
}

更新2: 一般Parse.com示例

Parse.com示例:

enter image description here

示例视图控制器: (在此视图控制器中,我正在检索多个对象并为每个对象设置视图控制器的文本字段,还有其他PFQueries,包括仅提取第一个对象或通过它的Id检索对象)

import UIKit
import Parse

class SOTestViewController: UIViewController {

@IBOutlet weak var venueName: UITextField!
@IBOutlet weak var venuePhone: UITextField!
@IBOutlet weak var venueAddress: UITextField!

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

func fetchVenuesFromParse() {

    let queryVenues = PFQuery(className: "Venues") // my example class on Pasre.com

    // I want to get all Venue objects
    queryVenues.findObjectsInBackgroundWithBlock { (foundObjects: [AnyObject]?, error: NSError?) -> Void in

        // Things that happen inside of these {} happen in the background and may take some time to update from Parse.com

        for foundObject in foundObjects as! [PFObject] { // foundObjects needs to be cast back into PFObjects

            // Access each row on Parse as a foundObject inside this for loop

            //Just for an example, update this view controller's text fields from the data from Parse.com

            self.venueName.text      = foundObject["name"] as! String //Use subscripting on the foundObject and set the type of data
            self.venuePhone.text     = foundObject["phone"] as! String
            self.venueAddress.text   = foundObject["address"] as! String

        }

    }

}

}