下面是我用来从Parse查询对象的代码:
func findEmployeeForLoggedInUser(completion: (array: [AnyObject], error: String?) -> Void) {
var query = PFQuery(className: "Employee")
query.whereKey("employerId", equalTo: PFUser.currentUser()!.objectId!)
query.findObjectsInBackgroundWithBlock { (results, error) -> Void in
println(results)
var employeeArray = results
if let error = error {
let errorString = error.userInfo?["error"] as? String
if let objects = employeeArray {
completion(array: objects, error: errorString)
} else {
completion(array: [], error: errorString)
}
} else {
completion(array: employeeArray!, error: nil)
}
}
}
下面是声明PFObject的子类的代码:
class PFEmployee: PFObject, PFSubclassing {
override class func initialize() {
self.registerSubclass()
}
class func parseClassName() -> String {
return "Employee"
}
@NSManaged var name: String
@NSManaged var jobDesc: String
@NSManaged var numberOfPoints: Int
@NSManaged var education: String
@NSManaged var birthday: String
@NSManaged var employerId: String
@NSManaged var profilePicture: PFFile?
@NSManaged var email: String
@NSManaged var commentary: String
}
我的应用程序崩溃,我怀疑它与此有关。我是否正确地做了这一切?
答案 0 :(得分:0)
使用此无崩溃代码...
var stuffarray = [String]()
var query = PFQuery(className:"ClassName")
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
// The find succeeded.
println("Successfully retrieved \(objects!.count) scores.")
// Do something with the found objects
if let objects = objects as? [PFObject] {
for object in objects {
stuffarray.append(object.valueForKey("Column")! as! String)
}
}
} else {
// Log details of the failure
println("Error: \(error!) \(error!.userInfo!)")
}
dispatch_async(dispatch_get_main_queue()) {
self.alert("\(stuffarray)", Message: "")
}
}