ObjectIDQuery.findObjectsInBackgroundWithBlock causing Xcode source editor to have limited functionality - swift

I am attempting a query from Parse to get the object ids in an array and not have to hard code the ids. I attempt to use the following code:
var ObjectIDQuery = PFQuery(className: "QuestionsandAnswers")
ObjectIDQuery.findObjectsInBackgroundWithBlock({
(objectsArray : [AnyObject]?, error : NSError?) -> Void in
var ObjectIDs = objectsArray as! [PFObject]
for i in 0..<ObjectIDs.count{
self.ObjectIDsPublicArray.append(ObjectIDs[i].objectId)
}
})
But the code causes Xcode to state "Xcode encountered a problem. Source editor functionality is limited.Attempting to restore"
Anyone know why that code would cause that? Also any suggestions to fix?

you are on the right path but Parse doesn't use [AnyObject]? anymore in their new SDK so change to [PFObject]?
Example:
let objectIdQuery = PFQuery(className: "QuestionsandAnswers")
objectIdQuery.findObjectsInBackgroundWithBlock({
(objectsArray : [PFObject]?, error : NSError?) -> Void in
if error == nil
{
if let objects = objectsArray
{
for one in objects
{
let objectID = one.objectID //<--- objectID
// then append the objectID into your data structure
}
}
}
})

This is a common problem, basically parse updated their SDK
Just change
[AnyObject]? to [PFObject]?
Same problem here I think
PFArrayResultBlock(parse) is causing an error while converting to swift 2.0

Related

What am I doing wrong downloading data from parse?

I am trying to download some data from parse but I get an error message saying "Value of type 'PFObject' has no member 'name' What am I doing wrong?
here is my parse dashboard screenshot
here is my code to upload the data to parse:
var coordinates = PFGeoPoint (latitude: (newCoordinate2.latitude), longitude:(newCoordinate2.longitude))
var aboutSpot = PFObject(className: "spotdetail")
aboutSpot ["PFGeoPoint"] = coordinates
aboutSpot["name"] = "name"
aboutSpot.saveInBackgroundWithBlock { (succes, error) -> Void in
print("separate name and geopoint have been saved")
}
and here is my code to download my data:
var query = PFObject.query()
query!.findObjectsInBackgroundWithBlock ({ (objects, error) in
if let places1 = objects {
for object in places1 {
if let spotdetail = object as? PFObject {
self.rideSpots.append(spotdetail.name!)
}
}
}
print(self.rideSpots)
})
also not that on the line that says
if let spotdetail = object as? PFObject {
I get a warning saying "conditional cast from 'PFObject' to 'PFObject' always succeeds
I can probably solve this pretty easily but I wanted to mention it in case it could help solve the issue

Cannot Subscript A PFObject Error

I've attempted to solve this error, but I've had no luck in doing so. I'm getting the error: Cannot subscript a value of type '[PFObject]' with an index of type 'String' On this line of code: self.postDates.append(posts["createdAt"] as! String).
This is the portion of code I'm having trouble with:
var posts : [Post] = []
var postDates = [String]()
func loadData() {
var query = PFQuery(className: "Post")
query.orderByDescending("createdAt")
query.findObjectsInBackgroundWithBlock {(posts: [PFObject]?, error: NSError?)-> Void in
if error == nil {
if let posts = posts {
for post in posts {
self.postDates.append(posts["createdAt"] as! String)
}
self.tableView.reloadData()
}
} else {
// is an error
}
}
}
I'm trying to get the date and then display it every time the user create a new post utilizing Parse. Can anyone explain what is going on?
This is the tutorial I'm following along with: https://www.youtube.com/watch?v=L3VQ0TE_fjU
Because posts is an array of PFObject, how can you get an element inside from String? It's supposed to be an Int. It's just your typo, you already knew what you are doing. post is the PFObject you want.
for post in posts {
self.postDates.append(post["createdAt"] as! String)
}
You are trying to get (and add) the created at date of the PFObject,
instead you are getting the date of and array of PFObject (Which Posts is).
You should try to get the elements in the array, and get the date from the element instead of the array.
for post in posts{
postDates.append(post["createdAt"] as! String)
}

pointers in parse class with swift code

I'm trying to insert a text under description column, and it keep giving me error, I think I'm having this issue because I wasn't able to use the pointer properly.. is there any one can help, I watched a number of tutorial video's as to how to setup the pointer in Parse, and that didn't solve the issue.
Thanks.
let me = self.textField.text
let query = PFQuery(className: "Store")
query.getObjectInBackgroundWithId ("product", block: {
(object: PFObject?, error: NSError?) -> Void in
if error != nil
{
print(error)
}
else if let transaction = object {
transaction["discription"] = "\(me)"
transaction.saveInBackground()
print(object!.objectForKey("discription"))
}
})
I finally got the answer, the best possible answer I got was, not store any information to the DB till all the input is done from the last page. It means you carry over the input as temp to each pages, then save it at the end... :)

Value of optional type PFQuery? not unwrappped

I updated to the latest version of Swift/XCode and a PFQuery in my app is generating an error: Value of optional type 'PFQuery?' not unwrapped. I know I could add a bang (!) but this only makes the error goes away. It doesn't actually fix the problem. The query used to return results before I upgraded. Here's the code in question:
PFGeoPoint.geoPointForCurrentLocationInBackground { (geopoint, error) -> Void in
println(error)
if error == nil {
println(geopoint)
if var user = PFUser.currentUser(){
user["location"] = geopoint
var query = PFUser.query()
query.whereKey("location", nearGeoPoint:geopoint) //error on this line
query.limit = 10
query.findObjectsInBackgroundWithBlock({ (users, error) -> Void in
Instead of simply making it query!.whereKey(etc) what's the best way to fix this?
Thanks!
Thank you for asking, as it is all too tempting just to force-unwrap. Do it like this:
if var query = PFUser.query() {
query.whereKey("location", nearGeoPoint:geopoint)
// ... and so on
}
(It is odd that you do not know this, since you are doing it correctly just two lines before with PFUser.currentUser(). A case of the left brain not knowing what the right brain is doing?)

Swift querying Parse all objects in class

I have an app that I need to grab all values in the class. I need to get "players" and "total" from the class "runningTotal". Here is the code I have:
var query = PFQuery(className:"runningTotal")
query.selectKeys(["players", "total"])
query.findObjectsInBackgroundWithBlock
{
(objects: [AnyObject]!, error: NSError!) -> Void in
if error == nil
{
self.test = objects[0]["total"]
}
}
I want to set a variable (test) equal to the result of total. I would also want to do this with players. I don't think the above code is right, as it doesn't work. I obviously don't need any constraints as I want to fetch all of the results from this class. How would I go about solving this?
Thanks for any help in advance!
As long as your query is error free, you'll need to iterate through the objects array. As you iterate through each object, which will be of type AnyObject, you will need to cast the object as a PFObject. Then you will be able to grab the data you require from it.
var query = PFQuery(className:"runningTotal")
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]!, error: NSError!) -> Void in
if error == nil{
for object in objects{
if let data = object as! PFObject{
//Set test to total (assuming self.test is Int)
self.test = data["total"] as! Int
}
}
}else{
//Handle error
}
}