How to get array of array from parse - ios

I have stored a array of array in parse in a column named "Polygon" like this:
[[13.74653990489156,100.4923625509455],[13.74652348443379,100.4925687879127],[13.74636831105128,100.4925992163138],[13.74660230262049,100.4924453837896]]
But I am unable to access those data =rom parse.
I have tried this method:
private func gettingPolygonsFromParse() {
let query = PFQuery(className: "AttractionPolygon")
query.findObjectsInBackgroundWithBlock { (polygonsArray, error) -> Void in
if error == nil {
for polygon in polygonsArray as! [PFObject] {
// I am unable to get polygon value from here.
}
}
}
}

It's unclear if Polygon column belongs to AttractionPolygon class.
If yes, try this:
for polygon in polygonsArray as! [PFObject] {
if let array1 = polygon["Polygon"] as? [[Double]] {
for item in array1 {
print(item)
}
}
}

Related

Appending into an array from completion block

I am attempting to append the results of the parse query into usersData
struct Data {
var FirstName:String!
var LastName:String!
var Gender:String!
var Age:String!
}
In the class I have
var usersData = [Data]()
I am using this to
func parseUsersData(completionHandler: [Data] -> Void) {
var usersDataArray = [Data]()
let query = PFQuery(className: "_User")
query.fromLocalDatastore()
query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
if error == nil {
if let user = objects as? [PFObject]! {
for object in user! {
var singleData = Data()
singleData.FirstName = object["firstName"] as! String
singleData.LastName = object["lastName"] as! String
singleData.Gender = object["gender"] as! String
singleData.Age = object["age"] as! String
usersDataArray.append(singleData)
}
}
completionHandler(usersDataArray)
}
}
}
finally, I am trying to do this:
Edit: To clarify, I need to pass the data from the queries, userDataArray, into the array usersData.
parseUsersData { (usersDataArray) -> Void in
usersData.append(usersDataArray)
}
The error I am getting is
Cannot convert value of type '[Data]' to expected argument type 'Data'
In your last code block you seem to appending a Data array to a Data object usersData.append(usersDataArray) which causes error I think. Did you mean to write usersDataArray.append(usersData) which makes a lot more sense?
You are attempting to add an array of Data to an array that is looking for Data Objects. Add a new struct that handles your Data Array and then change the usersData to look for DataArray's:
struct DataArray {
var array = [Data]()
}
And change the line:
var usersData = [DataArray]()
usersDataArray is an array. To add an array to an other array, prefer appendContentsOf :
usersData.appendContentsOf(usersDataArray)

how to retrive data from a user pointer on Parse using swift

On Parse I have users with Facebook profile and Email login profile. So I want to bury for users data in my twitter-like app.
In my "messages" class on Parse I have column "sender" that contains pointers to parse users.
I just want to retrive and show the name of users in class "messages" contained in the column "sender" wich contains pointers to PFUsers of which I need data for keys
"first_name"
"last_name"
"profile_picture"
How can I retrive their data like name and image in order to show them in a tableview?
these are the declarations of my arrays:
var sendersArray : [String] = []
var picturesArray : [NSData] = []
maybe I could use something like this tuple, but I can't understand how to grab data from pointers
for user in list {
let firstName = "fist_name"
let lastName = "last_name"
let oProfileImage = NSData() //"image_profile" as! NSData
otherUsers.append((oName: firstName, oLastName: lastName, oImageProfle: oProfileImage))
}
version - 1:
I started with printing the whole pf object
//******************************************************
func theSearch() {
let theSearchQuery = PFQuery(className: "Messages")
theSearchQuery.findObjectsInBackgroundWithBlock({
(objects : [AnyObject]?, error : NSError?) -> Void in
for object in objects! {
let theName = object.sender!
print(object)
print(theName)
sendersArray.append(theName)
let profilePicture = object["profile_pic"] as! PFFile
picturesArray.append(profilePicture)
}
self.tableView.reloadData()
})
}
//*******************************************************
version - 2:
then, found this solution, but still, doesn't
func theSearch() {
let theSearchQuery = PFQuery(className: "Messages" )
theSearchQuery.includeKey("sender")
theSearchQuery.findObjectsInBackgroundWithBlock({
(objects : [AnyObject]?, error : NSError?) -> Void in
for object in objects! {
let theName = object.sender!["first_name"] as? String
print(object)
print(theName)
sendersArray.append(theName)
let profilePicture = object["profile_pic"] as! PFFile
picturesArray.append(profilePicture)
}
self.tableView.reloadData()
})
}
errors:
seems to be a problem with sender, maybe I shouldn't use it
thanks in advance
let theName = object.objectForKey("sender")!.objectForKey("first_name") as! String
Complete Code:
func theSearch() {
let theSearchQuery = PFQuery(className: "Messages")
theSearchQuery.includeKey("sender")
theSearchQuery.findObjectsInBackgroundWithBlock({
(objects : [AnyObject]?, error : NSError?) -> Void in
for object in objects! {
let theName = object.objectForKey("sender")!.objectForKey("first_name") as! String
print(object)
print(theName)
self.sendersArray.append(theName)
let profilePicture = object["profile_picture"] as! PFFile
self.picturesArray.append(profilePicture)
}
self.tableView.reloadData()
})
}
Also, your picturesArray should be of type PFFile, like this:
var picturesArray = [PFFile]()
NOT NSData. change that at the top of your class.
-----EDIT------:
If you want to retrieve an image from a parse query, do this:
1) at the top of your class, declare the following arrays to store the results:
// your images will be stored in the file array
var fileArray = [PFFile]()
// your first and last names will be stored in String Arrays:
var firstNameArray = [String]()
var lastNameArray = [String]()
2) perform the query:
let query1 = PFQuery(className: "_User")
query1.orderByDescending("createdAt")
query1.findObjectsInBackgroundWithBlock({
(objects : [AnyObject]?, error : NSError?) -> Void in
if error == nil {
for x in objects! {
let firstName = x.objectForKey("first_name") as! String
let lastName = x.objectForKey("last_name") as! String
self.firstNameArray.append(firstName)
self.lastNameArray.append(lastName)
if x.objectForKey("profile_picture") as? PFFile == nil {
print("do nothing cause it's nil")
}
else {
let file:PFFile = x.objectForKey("profile_image") as! PFFile
self.fileArray.append(file)
}
}
self.tableView.reloadData()
}
})
Note I am using Swift 2 and Xcode 7. Syntax is slightly different in Xcode 6.4 and Swift 1.2.

Convert String to PFObject.. Parse

I am retrieving all the user details this way..
var userIds = [String]()
var userNames = [String]()
var profilePics = [PFFile]()
var gender = [String]()
override func viewDidLoad() {
super.viewDidLoad()
var userQuery = PFUser.query()
userQuery?.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
if let objects = objects {
self.userIds.removeAll(keepCapacity: true)
self.userNames.removeAll(keepCapacity: true)
self.profilePics.removeAll(keepCapacity: true)
for object in objects {
if let user = object as? PFUser {
if user.objectId != PFUser.currentUser()?.objectId {
self.userIds.append(object.objectId as String!)
self.userNames.append(object["fullName"] as! String!)
self.profilePics.append(object["profilePicture"] as! PFFile!)
self.gender.append(object["gender"] as! String!)
}
}
}
}
self.tableView.reloadData()
})
}
now i have "follow" button in myCell.. saving this way..
#IBAction func followButtonTapped(sender: UIButton) {
var followers:PFObject = PFObject(className: "Followers")
followers["user"] = userIds[sender.tag] // Problem is here.. i want "user" column to be filled with userIds of users as Pointers not Strings. What should i do here??
followers["follower"] = PFUser.currentUser()
followers.saveInBackground()
}
Because i want to make relations with every object in "Followers" class that's why i want them to be in the form of Pointers.. Any suggestion please??
If you want to create a pointer to another PFObject (including PFUser) you will need to actually point to that object.
I see in your query that you are not adding any PFUsers to your array, but you are getting their object IDs.
Since a tag is only for an Int, you can not use it to pass a String (which a Parse objectID is).
You can subclass UIButton and create a new property to handle the objectID from parse and then use that to perform a query with the objectID and save the pointer to the result of that query. Or you can just add the object from your first query to your local array and use your button's tag to get the index of the object you want to point to.
Update
Since you have the objectId for the PFuser you want, you can get it and update your followers like so:
let getOjbectByIdQuery = PFQuery(className: "User")
getOjbectByIdQuery.whereKey("objectId", equalTo: userIds[sender.tag])
getOjbectByIdQuery.getFirstObjectInBackgroundWithBlock { (foundObject: PFObject?, error: NSError?) -> Void in
if let object = foundObject {
var followers:PFObject = PFObject(className: "Followers")
followers["user"] = object
followers["follower"] = PFUser.currentUser()
follwers.saveInBackground()
}
}

How to convert a String to PFObject

I've saved userIds in a column in parse as String and i want to convert it in PFObject . How can i do it in swift? I was trying to retrieve the userIds as follows:
followedUserQuery.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
if let objects = objects {
for object in objects {
var followedUser = object["user"] as! PFObject
and i am getting this error:
Could not cast value of type '__NSCFString' (0x105f94c50) to 'PFObject' (0x104708998).
thanks for your time.
Try this
followedUserQuery.findObjectsInBackgroundWithBlock { (objects:[AnyObject], error:NSError) -> Void in
if error == nil{
if let objects = objects as! [PFObject] {
for object in objects {
var followedUser = object["user"]
}
}

Getting data from Parse with findObjectsInBackgroundWithBlock

I'm using Parse findObjectsInBackgroundWithBlock that returns [AnyObject]? how do I extract the column data?
You need to cast your results like so:
objects as? [PFObject], then each result will contain a PFObject dictionary with the column name as the key. for example o["id"] will return the value of the id column for a specific object
Just do
let data = objects as! [PFObject]
let firstObject = objects[0]
// firstObject["Column"]
Well it is easy like the code below,
In my case i get the score you can use your DB table column names
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
if let objects = objects as? [PFObject] {
for object in objects {
var score : Int? = object["score"] as! Int?
}
} else {
// Log details of the failure
println("Error: \(error!) \(error!.userInfo!)")
}
}

Resources