【问题标题】:Cannot assign a value of type 'PFObject' to a value of 'PFObject!', Swift, Xcode无法将“PFObject”类型的值分配给“PFObject!”的值,Swift,Xcode
【发布时间】:2015-05-04 23:09:27
【问题描述】:

Xcode 6.3.1:错误:无法将“PFObject”类型的值分配给“PFObject!”的值

试图理解更严格的 Swift '!'和 '?'要求,因为我认为这是错误的主要原因。

问题在于以下代码行:

detailScene.currentObject = objects[row] as! PFObject

在:

import UIKit

class TeamsViewController: PFQueryTableViewController {

    // Initialise the PFQueryTable tableview
    override init(style: UITableViewStyle, className: String!) {
        super.init(style: style, className: className)
    }

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)

    // Configure the PFQueryTableView
    self.parseClassName = "Teams"
    self.textKey = "teamName"
    self.pullToRefreshEnabled = true
    self.paginationEnabled = false
}

// Define the query that will provide the data for the table view
override func queryForTable() -> PFQuery {
    var query = PFQuery(className: "Teams")
    query.orderByAscending("teamName")
    return query
}

// MARK: - Table view data source

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject!) -> PFTableViewCell {

    var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! PFTableViewCell!
    if cell == nil {
        cell = PFTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
    }

    // Extract values from the PFObject to display in the table cell
    if let teamName = object["teamName"] as? String {
        cell?.textLabel?.text = teamName
    }

    if let coachName = object["coachName"] as? String {
        cell?.detailTextLabel?.text = coachName
    }

    return cell
}

// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    // Get the new view controller using [segue destinationViewController].
    var detailScene = segue.destinationViewController as! AddTeamsViewController

    // Pass the selected object to the destination view controller.
    if let indexPath = self.tableView.indexPathForSelectedRow() {
        let row = Int(indexPath.row)
        detailScene.currentObject = objects[row] as! PFObject
    }
}

override func viewDidAppear(animated: Bool) {

    // Refresh the table to ensure any data changes are displayed
    tableView.reloadData()
}

// Override to support rearranging the table view.
override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {

}

// Override to support conditional rearranging of the table view.
override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    // Return NO if you do not want the item to be re-orderable.
    return true
    }

}

任何关于我为什么会出现错误以及如何修复它的指示将不胜感激。谢谢

【问题讨论】:

    标签: ios xcode swift parse-platform


    【解决方案1】:

    您需要强制解开对象数组,因为它是任意对象的可选数组 ([AnyObject]?)

    所以你的代码应该是这样的:

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
            // Pass the selected object to the destination view controller.
            if let indexPath = self.tableView.indexPathForSelectedRow() {
                let row = indexPath.row
                detailScene.currentObject = objects![row] as! PFObject
            }
    
        }
    

    打开包装的节省方法是:

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
            // Pass the selected object to the destination view controller.
            if let indexPath = self.tableView.indexPathForSelectedRow() {
                let row = indexPath.row
                if let unwrappedobjects = objects {
                    if let yourobject = unwrappedobjects[row] as? PFObject {
                        detailScene.currentObject = yourobject
                    }
                }
            }
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多