如何将PFObject转换为字符串? - Swift 1.2 Xcode 6.4和Parse.com -

时间:2015-07-30 17:23:50

标签: ios xcode swift parse-platform optional

这是另一个问题的一部分,但由于原始问题得到了答案,如果我在这里以单向方式面对这个问题,将来会更清楚。

编辑:添加了一个异常断点,在runTime出现了这个:

enter image description here

我收到了这条消息:来自' String'不相关的类型' PFObject'总是失败

这里是警告屏幕截图enter image description here

控制台也说" 无法投放类型' PFObject' (0x10980e1e8)到' NSString' (0x10b8e78e0)。 (lldb)"

在我看来,我没有以正确的方式访问数据,但不知道在哪里

提前致谢

//
//  TimelineTVC2.swift
//  lug15ParseChat
//
//






import UIKit
import Parse

class TimelineTVC2: UITableViewController {


    var timelineData : [String] = []


    func loadData() {

        timelineData.removeAll(keepCapacity: true)          //erase previus contents

        var findTimeLineDataQuery = PFQuery(className: "Messages")

        findTimeLineDataQuery.findObjectsInBackgroundWithBlock({
            (objects : [AnyObject]?, error : NSError?) -> Void in

            if error == nil {
                for singleObject in objects! {
                    self.timelineData.append(singleObject as! String)
                }
                let reversedArray : Array = self.timelineData.reverse()  //remeber always!
                self.timelineData = reversedArray as Array

                self.tableView.reloadData()
            }
        })
    }



    //    MARK: Parse
    override func viewDidAppear(animated: Bool) {

        self.loadData()

        if PFUser.currentUser() == nil {
            var loginAlertController = UIAlertController(title: "Sign up / login", message: "please sign up or login", preferredStyle: UIAlertControllerStyle.Alert)

            loginAlertController.addTextFieldWithConfigurationHandler({
                textfField in
                textfField.placeholder = "Your username"
            })

            loginAlertController.addTextFieldWithConfigurationHandler({
                textfField in
                textfField.placeholder = "Your password"
                textfField.secureTextEntry = true
            })

            //            MARK: login action in the array
            loginAlertController.addAction(UIAlertAction(title: "Login Action", style: UIAlertActionStyle.Default, handler: {
                alertAction in
                let textFields : NSArray = loginAlertController.textFields!
                let usernameTextField : UITextField = textFields[0] as! UITextField
                let passwordTextField : UITextField = textFields[1] as! UITextField

                //MARK: Parse login problem - 15:39
                PFUser.logInWithUsernameInBackground(usernameTextField.text, password: passwordTextField.text){
                    (user: PFUser?, error: NSError?) -> Void in

                    if user != nil {
                        println("login success!")
                    } else {
                        println("login failed!")
                    }
                }
            }))

            //            MARK: sign up action in the array
            loginAlertController.addAction(UIAlertAction(title: "Sign up", style: UIAlertActionStyle.Default, handler: {
                alertAction in
                let textFields : NSArray = loginAlertController.textFields!
                let usernameTextField : UITextField = textFields[0] as! UITextField
                let passwordTextField : UITextField = textFields[1] as! UITextField

                var messageSender = PFUser() //16:42
                messageSender.username = usernameTextField.text
                messageSender.password = passwordTextField.text

                messageSender.signUpInBackgroundWithBlock({
                    (success: Bool, error: NSError?) -> Void in
                    if error == nil {
                        println("sign up successful")
                    } else {
                        //                        let errorString = error!.userInfo["error"] as! String
                        let errorString = error!.localizedDescription
                        println(errorString)
                    }
                })

            }))


            self.presentViewController(loginAlertController, animated: true, completion: nil)

        }

    }




    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem()
    }






    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }



    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Potentially incomplete method implementation.
        // Return the number of sections.
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete method implementation.
        // Return the number of rows in the section.
        return timelineData.count
    }




    //MARK: WARNING! Cast from 'String' to unrelated type 'PFObject' always fails
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell : SweetTableViewCell = tableView.dequeueReusableCellWithIdentifier("cellReuseID", forIndexPath: indexPath) as! SweetTableViewCell

        var textMessage : PFObject = self.timelineData[indexPath.row] as! PFObject

        cell.sweetTextView.text = textMessage["content"] as! String

        return cell
    }
}

2 个答案:

答案 0 :(得分:3)

您要做的是将整个PFObject转换为String,正如编译器指出的那样永远不会有效。您需要做的是访问包含您尝试获取的字符串的PFObject内的字段。例如,在User类上,电子邮件字段是一个字符串。为了从PFUserPFObject的子类)获取,您需要访问该字段,然后将 分配给字符串。它看起来像这样(假装singleObjectPFUser,因为我不知道你的字段):

for singleObject in objects! {
    if let stringData = singleObject["email"] as? String {
        timelineData.append(stringData)
    }
}

另一种选择,只是语法不同。

for singleObject in objects! {
    if let stringData = singleObject.objectForKey("email") as? String {
        timelineData.append(stringData)
    }
}

两者都能奏效并给你想要的结果。

此外,作为一个附加点,您可以简单地调用findTimeLineDataQuery.orderByDescending("createdAt"),而不是在将对象放入其中后反转数组。这将节省一些开销,因为它将在数据返回给您之前完成,而不是在设备上完成。

编辑:修复cellForRowAtindexPath:

 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell : SweetTableViewCell = tableView.dequeueReusableCellWithIdentifier("cellReuseID", forIndexPath: indexPath) as! SweetTableViewCell

    cell.sweetTextView?.text = self.timelineData[indexPath.row]

    return cell
}

您试图转换刚从PFObject中取出的字符串,然后将其转回PFObject。由于您现在要显示文本,因此在cellForRowAtindexPath:中您只需访问timelineData数组中该位置存储的值即可。

答案 1 :(得分:1)

你真正想要做的是投射PFObject属性 - 而不是对象本身。在下面的示例中,someProperty是您要访问的Messages对象上的字段。

看起来应该是这样的:

for singleObject in objects! {
    var someProperty = singleObject["somePropertyName"] as String
    self.timelineData.append(someProperty)
}