Swift [Error]: Object not found. (Code: 101, Version: 1.12.0) - swift

I have been trying to change the content in an array called testArray in the class Collabs and then save it back to the parse server.
#IBAction func addToArray(_ sender: AnyObject) {
var objectToSave: PFObject?
let query = PFQuery(className: "Collabs")
query.getObjectInBackground(withId: collabID) { (object, error) in
if error != nil {
print(error)
} else if let content = object {
objectToSave = content
}
if objectToSave != nil {
objectToSave!["testArray"] = ["foo","bar","foobar"]
objectToSave!.saveInBackground(block: { (success, error) in
if error != nil {
print("ERROR")
} else {
print("SUCCESS")
}
})
}
}
I've seen quite a few posts that talk about access rights however, to the best of my knowledge, the class Collabs has public read and write enabled

sorry I didnt check it that deeply before, try this
var objectToSave: PFObject?
let query = PFQuery(className: "Collabs")
query.getObjectInBackground(withId: collabID) { (object, error) in
if error != nil {
print(error)
} else {
if let content = object {
objectToSave = content
}
if objectToSave != nil {
objectToSave!["testArray"] = ["foo","bar","foobar"]
objectToSave!.saveInBackground(block: { (success, error) in
if error != nil {
print("ERROR")
} else {
print("SUCCESS")
}
})
}
}
}

Related

From server Class into an array Swift

I want to display my data I parse from server into my view collection, so I used PFQuery as the following
func LoadData(){
let query = PFQuery(className: "AntStore")
query.findObjectsInBackground {(returned, error) -> Void in
if error != nil {
print(error!)
} else {
if let data = returned {
for object in data {
self.TitlesArray.append(object.object(forKey: "Title") as! String)
print(self.TitlesArray)
}
}
}
}
}
I can't put it in a single array to send it to the view collection variable!! Any help ?
You are printing inside the loop, move the print statement outside the loop as follows:
func LoadData(){
let query = PFQuery(className: "AntStore")
query.findObjectsInBackground {(returned, error) -> Void in
if error != nil {
print(error!)
} else {
if let data = returned {
for object in data {
self.TitlesArray.append(object.object(forKey: "Title") as! String)
}
print(self.TitlesArray) //Moved print outside the loop
}
}
}
}

Value of type 'AVCapturePhotoOutput' has no member 'captureStillImageAsynchronously'

Please help me to refactor this code, it does not work, I have searched in the internet but did not found any solution. I could not refactor it by myself.
This line:
imageOutput.captureStillImageAsynchronously(from: connection) { (sampleBuffer, error) -> Void in
throws this error:
Value of type 'AVCapturePhotoOutput' has no member 'captureStillImageAsynchronously'
imageOutput.captureStillImageAsynchronously(from: connection) { (sampleBuffer, error) -> Void in
if (sampleBuffer == nil || error != nil) {
DispatchQueue.main.async {
completion(nil, error)
}
return
}
guard let data = AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: sampleBuffer!, previewPhotoSampleBuffer: nil) else {
DispatchQueue.main.async {
completion(nil, StillImageError.noData)
}
return
}
guard let image = UIImage(data: data) else {
DispatchQueue.main.async {
completion(nil, StillImageError.noImage)
}
return
}
var saver = ImageSaver()
.onSuccess { image, assetId in
completion(assetId, nil)
}
.onFailure { error in
}
saver = saver.save(image, filter: nil)
}
captureStillImageAsynchronously(from:completionHandler:) is a function of AVCaptureStillImageOutput, not AVCapturePhotoOutput.
Since AVCaptureStillImageOutput is deprecated since iOS 10, you should use AVCapturePhotoOutput instead, like you do in your code. It has a function with the same use case: capturePhoto(with:delegate:).
For more information about the usage of AVCapturePhotoOutput, check out this question.

In Swift, how do you check if pointer in Parse column is empty or not

Within my user object I added a column to add a users favorite team. The column is identified as favTeam and is a pointer to a teams class
Here is my code. I have populated my user with a favorite team however the logic is always showing that "favteam nil"
if let object = PFUser.currentUser()!["favTeam"] as? [PFObject]{
print("favteam not nil")
print(object)
let favTeam = PFUser.currentUser()!["favTeam"]
favTeamText.text = favTeam["Name"] as? String
if let favTeamImageView = favTeam["teamLogo"] as? PFFile {
favTeamImageView.getDataInBackgroundWithBlock { (imageData: NSData?, error: NSError?) -> Void in
if error == nil {
if let imageData = imageData {
self.teamLogo.image = UIImage(data: imageData)
}
}
}
}
}
else {
print("favteam nil")
}
I can accomplish this by using a PFUser.query() as follows...
func fetchFavoriteTeam() {
let userQuery: PFQuery = PFUser.query()!
userQuery.whereKey("username", equalTo: (currentUser?.username)!)
userQuery.findObjectsInBackgroundWithBlock({
(users, error) -> Void in
var favTeam = users!
if error == nil {
if favTeam != nil {
favTeamContainer = favTeam.valueForKey("favTeam") as! PFObject
}
} else {
print(error)
}
})
}

fatal error: unexpectedly found nil while unwrapping an Optional value. Swift

I am new in Swift. My question is I am not sure how to unwrapping the optional value. When I print the object.objectForKey("profile_picture"), I can see Optional(<PFFile: 0x7fb3fd8344d0>).
let userQuery = PFUser.query()
//first_name is unique in Parse. So, I expect there is only 1 object I can find.
userQuery?.whereKey("first_name", equalTo: currentUser)
userQuery?.findObjectsInBackgroundWithBlock({ (objects: [PFObject]?, error: NSError?) -> Void in
if error != nil {
}
for object in objects! {
if object.objectForKey("profile_picture") != nil {
print(object.objectForKey("profile_picture"))
self.userProfilePicture.image = UIImage(data: object.objectForKey("profile_pricture")! as! NSData)
}
}
})
You'd use if let to perform "optional binding", only performing the block if the result in question is not nil (and binding the variable profilePicture to the unwrapped value in the process).
It would be something like:
userQuery?.findObjectsInBackgroundWithBlock { objects, error in
guard error == nil && objects != nil else {
print(error)
return
}
for object in objects! {
if let profilePicture = object.objectForKey("profile_picture") as? PFFile {
print(profilePicture)
do {
let data = try profilePicture.getData()
self.userProfilePicture.image = UIImage(data: data)
} catch let imageDataError {
print(imageDataError)
}
}
}
}
Or, if you want to get data asynchronously, perhaps:
userQuery?.findObjectsInBackgroundWithBlock { objects, error in
guard error == nil && objects != nil else {
print(error)
return
}
for object in objects! {
if let profilePicture = object.objectForKey("profile_picture") as? PFFile {
profilePicture.getDataInBackgroundWithBlock { data, error in
guard data != nil && error == nil else {
print(error)
return
}
self.userProfilePicture.image = UIImage(data: data!)
}
}
}
}
It would be something along those lines, using if let to unwrap that optional. And you then have to get the NSData associated with that the PFFile object (from the getData method or getDataInBackgroundWithBlock, presumably).
See Optional Binding discussion in The Swift Programming Language.

How to convert AnyObject to PFObject/PFUser in swift?

I am setting up a PFRelation for the user variable and I'd like to add the text in a textfield as a PFRelation. First I query to see if the input username exists, then I make the returned object a PFUser. Then i attempt to add the PFUser to PFRelation and that's where it crashes, because AnyObject cannot be downcasted to PFUser/PFObject. How would I go about doing that?
var userQuery = PFUser.query()
userQuery.whereKey("username", equalTo: newUsername.text)
userQuery.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
if error == nil {
for object in objects{
if objects.count > 0 {
var PFRelation = PFUser.currentUser().relationForKey("friends")
var addRelation = object["username"] as PFUser
PFRelation.addObject(addRelation)
PFUser.currentUser().saveInBackgroundWithBlock{
(succeeded: Bool!, error: NSError!) in
if error == nil {
println("newuser added")
}
else {
println(error.userInfo)
}
Thanks!!
From my understanding it couldn't be done because PFObject/PFUser is like an array. Thus I couldn't cast just an object into an array. I also used getFirstObjectInBackgroundWithBlock() instead because it returns a PFObject instead of AnyObject, not sure if that made a difference. Below is my working code.
var userQuery = PFUser.query()
userQuery.whereKey("username", equalTo: newUsername.text)
userQuery.getFirstObjectInBackgroundWithBlock{ (object, error) -> Void in
if error == nil {
println(object)
var PFRelation = PFUser.currentUser().relationForKey("friends")
var addRelation = object
if addRelation != nil {
PFRelation.addObject(addRelation)
println("new relation added")
}
PFUser.currentUser().saveInBackgroundWithBlock{
(succeeded: Bool!, error: NSError!) in
if error == nil {
println("newuser saved")
}
else {
println(error.userInfo)
}
}
}
else {
println(error)
}
}