Get data from Parse Array into local CGFloat Array - swift

Im having trouble with Swift and Parse.com array because the documentation does not say much about the Syntax and im pretty new to Swift.
So what i want to do:
I want to add two values from two CGFloats to my Database.
Later i want to pull this two Values out from Parse Database and write them into a local CG Float array. But im Having a lot of trouble
Here is my Code for uploading the Value to the Parse.com Array: (This works fine, i have the three values in my Pase Database)
var positionAx : CGFloat = -1000
var positionAy : CGFloat = -1000
var post = PFObject(className: "Post")
post["antwortA_koord"] = [positionAx, positionAy, 0]
post.saveInBackgroundWithBlock{(success: Bool!, error: NSError!) -> Void in
if success == false {
self.displayAlert("Could not post your Question", error: "Please check your internet connection")
}
else {
println("You have posted")
}
And here the Code for Getting the data from Parse.com
var buttonA = [CGFloat] ()
var query = PFQuery(className:"Post")
query.whereKey("user", notEqualTo: PFUser.currentUser().username)
query.limit = 1
query.findObjectsInBackgroundWithBlock{
(objects: [AnyObject]!, error: NSError!) -> Void in
if error == nil {
for object in objects {
self.buttonA.append(object["antwortA_koord"] as CGFloat)
println(self.buttonA)
}
I get an error in this line: self.buttonA.append(object["antwortA_koord"] as CGFloat) but i dont know how to write the Syntax in the right way.
I hope you can help me

Related

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

Compound Query from loop - Parse Swift

In the app Im working on, users select a set of "tags" among a list, then I need to query all items which include at least one of those tags.
To do so, I want to use compound queries.
documentation from Parse.com
var lotsOfWins = PFQuery(className:"Player")
lotsOfWins.whereKey("wins", greaterThan:150)
var fewWins = PFQuery(className:"Player")
fewWins.whereKey("wins", lessThan:5)
var query = PFQuery.orQueryWithSubqueries([lotsOfWins, fewWins])
query.findObjectsInBackgroundWithBlock {
(results: [PFObject]?, error: NSError?) -> Void in
if error == nil {
// results contains players with lots of wins or only a few wins.
}
}
my issue here is that my tags are in an array, and I don't know how much there is (could be 1, could be 3). So I though about doing a for loop to create a query for every one of them.
my problem is that I can't access the variable outside of this for loop, and I can't declare them before hand as I don't know how much there will be.
something that would look like this.
for(var i = 0; i < tags.count; i++){
let i = PFQuery(className: "items")
i.whereKey("tags", equalTo: tags[i])
}
var query = PFQuery.orQueryWithSubqueries([variable1, variable2 ... ])
query.findObjectsInBackgroundWithBlock {
(results: [PFObject]?, error: NSError?) -> Void in
if error == nil {
// results
}
}
is there a way to achieve such thing?

ObjectIDQuery.findObjectsInBackgroundWithBlock causing Xcode source editor to have limited functionality

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

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
}
}

Extra argument in Call for Parse in Swift

All I am trying to get multiple objects out of a parse database.
Here is some of my code :
So this does the query :
var MainPicture = PFQuery(className: "Staff")
MainPicture.whereKey("Position", equalTo: "Sales Manager")
MainPicture.findObjectsInBackgroundWithBlock ({(objects:[AnyObject]!, error: NSError!) in
if(error == nil){
self.getMainImageData(objects as [PFObject])
}
else{
println("Error in retrieving \(error)")
}
Then I want to get a few rows out of the query :
func getMainImageData(objects: [PFObject]) {
for object in objects {
let MainPic = object["StaffPic"] as PFFile
let MainData = object["FirstName","SecondName","Position"] as PFFile
MainPic let works, but when I try and do multiple ones like MainData , I get an error : "Extra argument in call" .. I thought this would have worked.
I suspect you cannot subscript PFObject with multiple items. It is like calling
dictionary["key1", "key2"]
That will also result in too many arguments.
It is confusing that your variables are Capitalized. They look like class names.