【问题标题】:You need to specify a parseClassName for the PFQueryTableViewController您需要为 PFQueryTableViewController 指定一个 parseClassName
【发布时间】:2015-01-31 19:02:38
【问题描述】:

我创建了一个 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()

}

} `

【问题讨论】:

    标签: xcode swift parse-platform


    【解决方案1】:

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

    对象的类名。

    @property(强,只读)NSString *parseClassName

    声明于 PFObject.h

    Obj-C

    @implementation MYGame
    
     @dynamic title;
    
     + (NSString *)parseClassName {
         return @"Game";
     }
    
     @end
    

    斯威夫特

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

    【讨论】:

      【解决方案2】:

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

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

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

      【讨论】:

        猜你喜欢
        • 2010-09-27
        • 1970-01-01
        • 2014-10-05
        • 2020-01-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-07
        • 1970-01-01
        相关资源
        最近更新 更多