I have searched through a number of similar topics but have not found a solution as of yet. I am using Parse social and using the login files.
I get the following error:
"AnyObject?" is not convertible to 'String'
I am very new to Swift & Parse - I believe this is the correct method of retrieving data, so please correct me if I am wrong.
var userObjectID = PFUser.currentUser()!.objectId!
var query = PFQuery(className:"User")
query.getObjectInBackgroundWithId("\(userObjectID)") {
(userInfo: PFObject?, error: NSError?) -> Void in
if error == nil && userInfo != nil {
println(userInfo)
let userScore = userInfo["level"] as! String
} else {
println(error)
}
}
Below is the database on Parse
I think you need to unwrap the PFObject you receive:
let userScore = userInfo!["level"] as! String
Related
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
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)
}
I am trying to retrieve contact names, here's how:
func getContactNames() {
let adbk : ABAddressBook? = ABAddressBookCreateWithOptions(nil, nil).takeRetainedValue()
let people = ABAddressBookCopyArrayOfAllPeople(adbk).takeRetainedValue() as [ABRecord]
for person in people {
contactList.append(ABRecordCopyCompositeName(person).takeRetainedValue() as String)
}
}
When all contacts do have names it works, although when there are some contacts without names, app crashes and I get:
fatal error: unexpectedly found nil while unwrapping an Optional value
I tried using ? like this:
let contact2 = (ABRecordCopyCompositeName(person)?.takeRetainedValue() as? String)
if contact2 != nil {
contactList.append(contact2!)
}
Then I would always get nil.
Any ideas what I am doing wrong?
In my experience you have to do it step-by-step: first check if ABRecordCopyCompositeNameis not nil and then take it and convert to string.
if let tmpName = ABRecordCopyCompositeName(person) {
let contact2 = tmpName.takeRetainedValue() as String
contactList.append(contact2)
}
I'm doing a PFQuery to get uploaded file in column. How to get that file and if the column is null there's no file do something else ?
If the column is null i'm getting error message the query wont go well.
fatal error: unexpectedly found nil while unwrapping on Optional value
query.findObjectsInBackgroundWithBlock {
(objects, error) -> Void in
if error == nil
{
for object in objects! {
self.files.append(object.objectForKey("file") as! PFFile)
}
I want to get that file but if the column was null do something else ? how can i do that?
Use optional unwrapping with if let file = ... as? PFFile {
} else {
}
I'm trying to find the most efficient way to do a fetch request against CoreData. Previously I have first checked if an error existed, and if it did not I have checked the array of the returned entity. Is there a quicker way to do this. Is something like this an accepted way to do the request?
let personsRequest = NSFetchRequest(entityName: "Person")
var fetchError : NSError?
//Is it okay to do the fetch request like this? What is more efficient?
if let personResult = managedObjectContext.executeFetchRequest(personRequest, error: &fetchError) as? [Person] {
println("Persons found: \(personResult.count)")
}
else {
println("Request returned no persons.")
if let error = fetchError {
println("Reason: \(error.localizedDescription)")
}
}
Kind Regards,
Fisher
Checking the return value of executeFetchRequest() first is correct.
The return value is nil if the fetch failed, in that case the error
variable will be set, so there is no need to check if let error = fetchError.
Note that the request does not fail if no (matching) object exist.
In that case an empty array is returned.
let personRequest = NSFetchRequest(entityName: "Person")
var fetchError : NSError?
if let personResult = managedObjectContext.executeFetchRequest(personRequest, error: &fetchError) as? [Person] {
if personResult.count == 0 {
println("No person found")
} else {
println("Persons found: \(personResult.count)")
}
} else {
println("fetch failed: \(fetchError!.localizedDescription)")
}