【问题标题】:How can I query parse class for particular object?如何查询特定对象的解析类?
【发布时间】:2016-02-19 11:58:02
【问题描述】:

在我的ViewController 中,我有一个标签,显示在之前的ViewController 中选择的俱乐部名称。我现在需要从我的 parse.com 类创建一个查询,以检索与显示为标签的俱乐部名称匹配的对象的所有信息。我知道如何成功查询解析,但我发现很难找到一种方法将查询与标签字符串相匹配,因为标签字符串可能会有所不同,具体取决于在前一个视图中选择的俱乐部。

代码:

import UIKit
import Parse

class MenuController: UIViewController {

    @IBOutlet weak var clubLabel: UILabel!

    var clubName = String()

    override func viewDidLoad() {
        super.viewDidLoad()

        clubLabel.text = clubName
    }
}

Previous View 已查询解析以使用俱乐部注释填充地图,如下所示:

        let annotationQuery = PFQuery(className: "Clubs")
        annotationQuery.findObjectsInBackgroundWithBlock{
        (clubs, error) -> Void in
        if error == nil {
            // The find succeeded.
            print("Successful query for annotations")
            // Do something with the found objects
            let myClubs = clubs! as [PFObject]
            for club in myClubs {

                //data for annotation
                let annotation = MKPointAnnotation()
                 let place = club["location"] as? PFGeoPoint
                let clubName = club["clubName"] as? String
                let stadiumName = club["stadium"] as? String
                annotation.title = clubName
                annotation.subtitle = stadiumName
                annotation.coordinate = CLLocationCoordinate2DMake(place!.latitude,place!.longitude)

                //add annotations
                self.mapView.addAnnotation(annotation)

【问题讨论】:

  • 但是您已经拥有了用于填充标签的对象,那么您对它做了什么?使用现有对象...
  • 更多信息是什么?您是如何获得这些球杆的?为什么这些对象不包含您需要的数据(或与数据的关系)?
  • 上一个视图中的“俱乐部标签数组”是如何填充的?如果你从 Parse 得到它,你为什么不保留这个对象呢?

标签: ios database swift parse-platform


【解决方案1】:

我们将使用自定义注释而不是使用默认的MKPointAnnotation,以便于理解。


首先我们将在不同的文件中创建一个符合MKAnnotation 协议的类ClubAnnotation

import UIKit
import MapKit
import Parse

class ClubAnnotation: NSObject, MKAnnotation {

    //MARK: - Instance properties

    let club:PFObject

    //MARK: - Init methods

    init(club:PFObject) {
        self.club = club
    }

    //MARK: - MKAnnotation protocol conformance

    var title: String? { get {       
        return club["clubName"]
        }
    }

    var subtitle: String? { get {
        club["stadium"]
        }
    }

    var coordinate: CLLocationCoordinate2D { get {        
        return CLLocationCoordinate2D(latitude:  club["location"].latitude, longitude: club["location"].longitude)
        }
    }
}

如果 Club 对象的字段“体育场”、“俱乐部名称”或“位置”为空,我会让您处理错误,因此您可以决定如何显示它。


然后,在包含MKMapView 对象的类中(并且可能符合MKMapViewDelegate 协议),替换对查询结果的处理:

let annotationQuery = PFQuery(className: "Clubs")
annotationQuery.findObjectsInBackgroundWithBlock{
    (clubs, error) -> Void in
    if let clubs = clubs where error == nil {
        // The find succeeded.
        print("Successful query for annotations")
        for club in clubs  {
            self.mapView.addAnnotation(ClubAnnotation(club:club))
        }
    } else {
        // Handle the error
    }
}

您意识到我不必手动设置titlesubtitlecoordinate 属性,因为您的ClubAnnotation 对象根据MKAnnotation 协议的实现直接返回它。

仍然在这个类中,添加一个selectedClub 属性。此属性将用于保存所选注释中的俱乐部,并将传递给推送的视图控制器。

var selectedClub:PFobject!

在您的calloutAccessoryControlTapped 委托方法中,您可以从注释中获取您的 PFObject,并将其传递给一个新的 viewController 来做您想做的任何事情,而无需从 Parse 查询它:

    func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {

    mapView.deselectAnnotation(view.annotation, animated: true)

    if let annotation = view.annotation as? ClubAnnotation {            
       clubSelected = annotation.club
    }
}

您现在已经从自定义注释类中保存了俱乐部对象,并且您拥有对它的引用。如果你想展示或推送一个包含更多关于这个俱乐部的信息的新视图控制器,只需覆盖prepareForSegue 并将这个值设置为推送/展示的视图控制器。您无需查询俱乐部的其他字段,因为它们都是根据您的 Parse 请求加载的。

【讨论】:

  • clubSelected 应该是一个类属性并设置它,而不是一个局部变量。您仍然需要 prepareforsegue 方法才能将 clubSelected 变量传递给新的视图控制器。如果您需要解析俱乐部以外的数据,则需要执行其他请求。
  • 我将编辑我的答案以更清晰,请稍等;)
【解决方案2】:

只需将whereKey 添加到您的PFQuery

annotationQuery.whereKey(clubName, equalTo: "clubName")

希望这会有所帮助。

【讨论】:

  • 您的数据库中的空间是如何保存的?
  • 这一直对我有用...你的字符串开头有空格吗?
  • 你试图放入 whereKey 的字符串
  • 是否可以用另一个字符(即“the-club”)替换空格?您可以在查询对象时再次替换它。查看this link 替换字符
猜你喜欢
  • 2022-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-27
  • 2019-08-15
  • 2019-01-09
  • 2016-09-14
  • 1970-01-01
相关资源
最近更新 更多