当我运行我的项目时,我收到以下错误,线程1:EXC_BAD_INSTRUCTION(代码:EXC_I386_INVOP,子代码= 0x0。我将在下面发布代码,但有人知道如何解决这个问题吗?
它出现在 let type = PFUser.currentUser()![" Type"]为?串
import UIKit
import Parse
import ParseUI
class TableViewController: PFQueryTableViewController, PFLogInViewControllerDelegate, PFSignUpViewControllerDelegate {
var usersLocation: PFGeoPoint? {
didSet {
// This will reload the tableview when you set the users location.
// Handy if you want to keep updating it.z
if (tableView != nil) {
tableView.reloadData()
}
}
}
override func viewDidAppear(animated: Bool) {
}
override func viewDidLoad() {
super.viewDidLoad()
}
// 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 = "User"
self.textKey = "fullName"
self.pullToRefreshEnabled = true
self.paginationEnabled = false
}
// Define the query that will provide the data for the table view
override func queryForTable() -> PFQuery{
let type = PFUser.currentUser()!["Type"] as? String
let age = PFUser.currentUser()!["Age"] as? String
let fitnessLevel = PFUser.currentUser()!["fitnessLevel"] as? String
var query = PFQuery(className:"User")
query.whereKey("Type", equalTo: type!)
query.whereKey("Age", equalTo: age!)
query.whereKey("fitnessLevel", equalTo: fitnessLevel!)
// If we don't have a location, just query without it.
if let geoLocation = usersLocation {
query.whereKey("geoLocation", nearGeoPoint:geoLocation)
}
query.limit = 100
return query
}
//override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("CustomCell") as! CustomCell!
if cell == nil {
cell = CustomCell(style: UITableViewCellStyle.Default, reuseIdentifier: "CustomCell")
}
// Extract values from the PFObject to display in the table cell
if let name = object?["fullName"] as? String {
cell.name.text = name
}
if let type = object?["lookingFor"] as? String {
cell.type.text = type
}
if let location = object?["location"] as? String {
cell.location.text = location
}
let cover = object?["cover"] as! PFFile
cover.getDataInBackgroundWithBlock {
(imageData: NSData?, error: NSError?) -> Void in
if error == nil {
if let imageData = imageData {
let image = UIImage(data:imageData)
}
}
}
let profile_pic = object?["profile_pic"] as! PFFile
profile_pic.getDataInBackgroundWithBlock {
(imageData: NSData?, error: NSError?) -> Void in
if error == nil {
if let imageData = imageData {
let image = UIImage(data:imageData)
}
}
}
return cell
}
}

答案 0 :(得分:0)
显然currentUser()
是nil
,导致崩溃(!
)时发生崩溃。
使用可选绑定:
if let user = PFUser.currentUser() {
let type = user["Type"] as? String
let age = user["Age"] as? String
let fitnessLevel = user["fitnessLevel"] as? String
}