gpt4 book ai didi

ios - 无法将类型 'PFObject' 的值分配给 'PFObject!' 的值,Swift,Xcode

转载 作者:搜寻专家 更新时间:2023-10-31 23:03:35 25 4
gpt4 key购买 nike

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
}

}

任何关于为什么我在那里出错以及如何修复它的方向将不胜感激。谢谢

最佳答案

您需要强制展开对象数组,因为它是可选的任意对象数组 ([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
}
}
}

}

关于ios - 无法将类型 'PFObject' 的值分配给 'PFObject!' 的值,Swift,Xcode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30041533/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com