【发布时间】:2015-07-30 17:23:49
【问题描述】:
这是另一个问题的一部分,但由于最初的问题得到了答案,如果我在这里单方面面对这个问题,将来会更清楚。
编辑:添加了一个异常断点,在运行时出现了这个:
我收到此消息:从“字符串”转换为不相关类型“PFObject”总是失败
控制台还显示“无法将 '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
}
}
【问题讨论】:
标签: ios xcode swift parse-platform optional