Xcode 您需要为PFQueryTableViewController指定parseClassName

Xcode 您需要为PFQueryTableViewController指定parseClassName,xcode,swift,parse-platform,Xcode,Swift,Parse Platform,我创建了一个tableViewcontroller,并在story board中为其分配了自定义类:PFQueryTableViewController。然后,我还为它提供了parseClassName“userMessage”,由于某种原因,当我尝试运行应用程序时,总是会收到相同的错误消息:nsinternalinconsistenceexception',原因:'您需要为PFQueryTableViewController指定一个parseClassName。 我不明白为什么会出现这个错误,

我创建了一个tableViewcontroller,并在story board中为其分配了自定义类:
PFQueryTableViewController
。然后,我还为它提供了
parseClassName“userMessage”
,由于某种原因,当我尝试运行应用程序时,总是会收到相同的错误消息:
nsinternalinconsistenceexception',原因:'您需要为PFQueryTableViewController指定一个parseClassName。
我不明白为什么会出现这个错误,因为我显式地给了类一个parseClassName

以下是PFQueryTableViewController的关联代码:

导入UIKit
导入核心定位
导入解析

class TableViewController:PFQueryTableViewController,CLLocationManagerDelegate{

let userMessages = ["blah blahh blahhh", "Beep Beep Boop", "Beep Beep Bobbity boop"]

let locationManager = CLLocationManager()
var currLocation: CLLocationCoordinate2D?

override init!(style: UITableViewStyle, className: String!) {
    super.init(style: style, className: className)
}

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

    self.parseClassName = "userMessage"
    self.textKey = "text"
    self.pullToRefreshEnabled = true
    self.objectsPerPage = 40

}





private func alert(message: String){
    let alert = UIAlertController(title: "Uh-OH", message: message, preferredStyle: UIAlertControllerStyle.Alert)
    let action = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil)
    let cancel = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil)
    let settings = UIAlertAction(title: "Settings", style: UIAlertActionStyle.Default) {(action) -> Void in
        UIApplication.sharedApplication().openURL(NSURL(string: UIApplicationOpenSettingsURLString)!)
        return

    }

    alert.addAction(settings)
    alert.addAction(action)
    self.presentViewController(alert, animated: true, completion: nil)
}

override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.estimatedRowHeight = 120
    self.tableView.rowHeight = 120
    locationManager.desiredAccuracy = 100
    locationManager.delegate = self
    locationManager.requestWhenInUseAuthorization()
    locationManager.startUpdatingLocation()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
    alert("Cannot fetch your location!!")
}

override func queryForTable() -> PFQuery! {
    let query = PFQuery(className: "Messages")
    if let queryLoc = currLocation {
        query.whereKey("location", nearGeoPoint: PFGeoPoint(latitude: queryLoc.latitude, longitude: queryLoc.longitude), withinMiles: 1)
        query.limit = 40
        query.orderByDescending("createdAt")
    }else {
        query.whereKey("location", nearGeoPoint: PFGeoPoint(latitude: 37.41182, longitude: -121.941125), withinMiles: 1)
        query.limit = 40
        query.orderByDescending("createdAt")
    }

    return query
}

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
    locationManager.stopUpdatingLocation()
    if(locations.count > 0) {
        let location = locations[0] as CLLocation
        println(location.coordinate)
        currLocation = location.coordinate
    } else {
        alert("Cannot fetch your loation")
    }
}

override func objectAtIndexPath(indexPath: NSIndexPath!) -> PFObject! {
    var obj : PFObject? = nil
    if(indexPath.row < self.objects.count) {
        obj = self.objects[indexPath.row] as? PFObject
    }
    return obj
}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return userMessages.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath!, object: PFObject!) -> PFTableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as TableViewCell
    cell.messageText.text = object.valueForKey("text") as? String
    cell.messageText.numberOfLines = 0
    let views = object.valueForKey("count") as Int
    cell.numberOfViewsLabel.text = "\(views)"
    cell.numberOfViewsLabel.text = "\((indexPath.row + 1) * 5)"
    return cell
}

func addToViews(sender: AnyObject) {
    let hitPoint = sender.convertPoint(CGPointZero, toView: self.tableView)
    let hitIndex = self.tableView.indexPathForRowAtPoint(hitPoint)
    let object = objectAtIndexPath(hitIndex)
    object.incrementKey("count")
    object.saveInBackgroundWithBlock { (Bool, NSError) -> Void in
        //blahhh
    }
    self.tableView.reloadData()

}
let userMessages=[“呜呜呜呜”,“嘟嘟嘟嘟”Boop,“嘟嘟嘟嘟”Bobbity Boop”]
让locationManager=CLLocationManager()
var currLocation:CLLocationCoordinate2D?
重写初始化!(样式:UITableViewStyle,类名:String!){
super.init(样式:样式,类名:类名)
}
必需的初始化(编码器aDecoder:NSCoder){
super.init(编码者:aDecoder)
self.parseClassName=“userMessage”
self.textKey=“text”
self.pullToRefreshanbled=true
self.objectsPerPage=40
}
专用函数警报(消息:字符串){
let alert=UIAlertController(标题:“哦”,消息:消息,首选样式:UIAlertControllerStyle.alert)
let action=UIAlertAction(标题:“确定”,样式:UIAlertActionStyle.Default,处理程序:nil)
let cancel=UIAlertAction(标题:“取消”,样式:UIAlertActionStyle.cancel,处理程序:nil)
让设置=UIAlertAction(标题:“设置”,样式:UIAlertActionStyle.Default){(操作)->Void in
UIApplication.sharedApplication().openURL(NSURL(字符串:UIApplicationOpenSettingsURLString)!)
返回
}
alert.addAction(设置)
alert.addAction(操作)
self.presentViewController(警报、动画:true、完成:nil)
}
重写func viewDidLoad(){
super.viewDidLoad()
self.tableView.estimatedRowHeight=120
self.tableView.rowHeight=120
locationManager.desiredAccuracy=100
locationManager.delegate=self
locationManager.RequestWhenUseAuthorization()
locationManager.startUpdatingLocation()
}
重写函数didReceiveMemoryWarning(){
超级。我收到了记忆警告()
}
func locationManager(经理:CLLocationManager!,didChangeAuthorizationStatus:CLAuthorizationStatus){
警报(“无法获取您的位置!!”)
}
重写func queryForTable()->PFQuery{
let query=PFQuery(类名:“消息”)
如果让queryLoc=currLocation{
query.whereKey(“位置”,nearGeoPoint:PFGeoPoint(纬度:queryLoc.lation,经度:queryLoc.longitude),withinMiles:1)
query.limit=40
query.orderByDescending(“createdAt”)
}否则{
查询.whereKey(“位置”,近地球点:PFGeoPoint(纬度:37.41182,经度:-121.941125),以英里为单位:1)
query.limit=40
query.orderByDescending(“createdAt”)
}
返回查询
}
func locationManager(管理器:CLLocationManager!,didUpdateLocations位置:[AnyObject]!){
locationManager.StopUpdatengLocation()
如果(locations.count>0){
将位置=位置[0]设为CLLocation
println(位置坐标)
currLocation=location.coordinate
}否则{
警报(“无法获取您的贷款”)
}
}
重写func objectAtIndexPath(indepath:nsindepath!)->PFObject{
变量对象:PFObject?=nil
if(indexPath.rowInt{
返回1
}
重写func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
返回userMessages.count
}
重写func tableView(tableView:UITableView,cellforrowatinedexpath indepath:nsindepath!,object:PFObject!)->PFTableViewCell{
将cell=tableView.dequeueReusableCellWithIdentifier(“cell”,forIndexPath:indexPath)设为TableViewCell
cell.messageText.text=object.valueForKey(“文本”)为?字符串
cell.messageText.numberOfLines=0
将views=object.valueForKey(“count”)设为Int
cell.numberOfViewsLabel.text=“\(视图)”
cell.numberOfViewsLabel.text=“\((indexPath.row+1)*5)”
返回单元
}
func addToViews(发送方:AnyObject){
让hitPoint=sender.convertPoint(CGPointZero,toView:self.tableView)
设HITDINDEX=self.tableView.indexPathForRowAtPoint(hitPoint)
让对象=对象索引路径(HITDINDEX)
对象。递增键(“计数”)
object.saveInBackgroundithBlock{(Bool,NSError)->中的Void
//废话
}
self.tableView.reloadData()
}
}
`parseClassName是一个只读变量,仅在子类化PFObject时使用。

对象的类名

@属性(强,只读)NSString*parseClassName

声明于 PFObject.h

Obj-C

迅捷的


在我的例子中,我使用了一个故事板,需要在我的
PFQueryTableViewController
子类中创建一个
initWithCoder:
方法。Parse.com文档中指向的模板缺少此方法,但示例后面的第一条注释包含示例实现:

正在生成消息“您需要为PFQueryTableViewController指定parseClassName”,因为没有任何方法正在设置
PFQueryTableViewController
parseClassName
属性。您将注意到,该属性在文档中提供的
initWithStyle:
方法示例中定义得非常清楚。但是,如果通过情节提要加载视图,则不会调用该方法:为此,需要在
initWithCoder:
methodo中设置
parseClassName
@implementation MYGame

 @dynamic title;

 + (NSString *)parseClassName {
     return @"Game";
 }

 @end
class MYGame: PFObject  {
    class func parseClassName() -> String! {
        return "Game"
    }
}