您需要为PFQueryTableViewController指定parseClassName

时间:2015-01-31 19:02:38

标签: xcode swift parse-platform

我创建了一个tableViewcontroller并在故事板中为其分配了自定义类:PFQueryTableViewController。然后我还给它parseClassName "userMessage",由于某种原因,当我尝试运行应用程序时,我总是得到相同的错误消息:NSInternalInconsistencyException', reason: 'You need to specify a parseClassName for the PFQueryTableViewController. 我不明白为什么我收到此错误,因为我明确地给了一个类parseClassName。

这是我关联的PFQueryTabletableViewController代码:

import UIKit import CoreLocation import Parse

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()

}

} `

2 个答案:

答案 0 :(得分:0)

parseClassName 是一个只读变量,仅在子类化PFObject时使用。 https://parse.com/docs/ios/api/Classes/PFObject.html#//api/name/parseClassName

  

对象的类名。

     

@property(strong,readonly)NSString * parseClassName

     

宣布进入   PFObject.h

的OBJ-C

@implementation MYGame

 @dynamic title;

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

 @end

夫特

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

答案 1 :(得分:0)

在我的情况下,我使用了故事板,需要在我的initWithCoder:子类中创建PFQueryTableViewController方法。 Parse.com文档中指向的模板缺少此方法,但示例后面的第一条评论确实包含一个示例实现:https://gist.github.com/jamesyu/ba03c1a550f14f88f95d#gistcomment-74202

正在生成消息“您需要为PFQueryTableViewController 指定parseClassName”,因为没有一种方法正在设置PFQueryTableViewController的{​​{1}}属性。您会注意到文档中提供的parseClassName方法示例中的属性非常明确地定义了。但是,如果通过故事板加载视图,则不会调用方法:为此,您需要在initWithStyle:方法中设置parseClassName

此外,不要混淆initWithCoder:的{​​{1}}子类。对于PFObject,您需要创建一个名为PFQueryTableViewController的类方法,并在调用[Parse setApplicationId:aid clientKey:ckey]之前注册子类。你没有为PFQueryTableViewController或任何其他ParseUI视图控制器做这些事情。它们依赖于一种或多种PFObject方法。