# Deleting / setting a pointer to nil

**URL:** <https://community.parseplatform.org/t/deleting-setting-a-pointer-to-nil/2148>\
**Category:** ParseSwift SDK\
**Tags:** pointers\
**Created:** [November 5, 2021, 5:26pm UTC](https://community.parseplatform.org/t/deleting-setting-a-pointer-to-nil/2148 "2021-11-05T17:26:28Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jayson](https://community.parseplatform.org/user_avatar/community.parseplatform.org/jayson/32/679_2.png) [@jayson](https://community.parseplatform.org/u/jayson)\
**Post date:** [November 5, 2021, 5:26pm UTC](https://community.parseplatform.org/t/deleting-setting-a-pointer-to-nil/2148/1 "2021-11-05T17:26:28Z")

</div>

Hi, I need help trying to understand if I’m doing something wrong here. Whatever I’ve tried, I can’t seem to remove a pointer from my object and set it to nil.

```auto
  func removeProfilePhoto(for userProfile: UserProfile) async throws -> UserProfile? {
    var workingProfile = userProfile
    workingProfile.photo = nil
    let savedProfile = try await workingProfile.save()
    return savedProfile
  }

```

photo is a pointer to another object

![Screen Shot 2021-11-06 at 1.19.33 AM](https://parse-community-forum-uploads.s3.dualstack.us-east-1.amazonaws.com/original/1X/92414925bd0a16a152101d4e8b04b6676310f759.png)

and every time I run this function, I check the dashboard but alas, the photo pointer is still there.

is there a special way to remove pointers from an object? I’ve tried googling but I nothing.

thanks in advance.

---

<div class="post-metadata">

**Author:** ![cbaker6](https://community.parseplatform.org/user_avatar/community.parseplatform.org/cbaker6/32/114_2.png) [@cbaker6](https://community.parseplatform.org/u/cbaker6)\
**Post date:** [November 5, 2021, 6:43pm UTC](https://community.parseplatform.org/t/deleting-setting-a-pointer-to-nil/2148/2 "2021-11-05T18:43:16Z")

</div>

The reason why setting the value to `nil` doesn’t work is here:

> <https://github.com/parse-community/Parse-Swift/issues/264#issuecomment-949129279>
>
> \### New Issue Checklist
> \<!--
> Check every following box \[x\] before submitti…ng your issue.
> Click the "Preview" tab for better readability.
> Thanks for contributing to Parse Platform!
> \--\>
> 
> \- \[x\] I am not disclosing a \[vulnerability\](https://github.com/parse-community/Parse-Swift/security/policy).
> \- \[x\] I am not just asking a \[question\](https://github.com/parse-community/.github/blob/main/SUPPORT.md).
> \- \[x\] I have searched through \[existing issues\](https://github.com/parse-community/Parse-Swift/issues?q=is%3Aissue).
> \- \[x\] I can reproduce the issue with the latest versions of \[Parse Server\](https://github.com/parse-community/parse-server/releases) and the \[Parse Swift SDK\](https://github.com/parse-community/Parse-Swift/releases). 
> 
> \### Issue Description
> Setting a value on an optional model property doesn't propagate to the server.
> 
> \### Steps to reproduce
> \`\`\`swift
> struct TestObject: ParseObject {
> // ParseObject Conformance
> var objectId: String?
> var createdAt: Date?
> var updatedAt: Date?
> var ACL: ParseACL?
> 
> var testValue: String?
> }
> 
> var testObj = TestObject()
> testObj.testValue = "value"
> 
> testObj = try! testObj.save()
> // Break here and check value on dashboard
> 
> testObj.testValue = nil
> 
> testObj = try! testObj.save()
> // Break here and check value again on dashboard, see that it still says "value" and not (undefined)
> \`\`\`
> 
> \### Expected Outcome
> Final value on dashboard should be (undefined) or nil, instead of keeping the old value.
> 
> \### Environment
> 
> Client
> \- Parse Swift SDK version: \`2.0.2\`
> \- Xcode version: \`13.0\`
> \- Operating system (iOS, macOS, watchOS, etc.): \`macOS\`
> \- Operating system version: \`11.6\`
> 
> Server
> \- Parse Server version: \`4.10.4\`
> \- Operating system: \`Ubuntu\`
> \- Local or remote host (AWS, Azure, Google Cloud, Heroku, Digital Ocean, etc): \`Digital Ocean\`
> 
> Database
> \- System (MongoDB or Postgres): \`MongoDB\`
> \- Database version: \`4.4\`
> \- Local or remote host (MongoDB Atlas, mLab, AWS, Azure, Google Cloud, etc): \`Digital Ocean\`

Follow the procedure [here](https://github.com/parse-community/Parse-Swift/pull/268#issuecomment-955714414) to set a property to nil. Basically, you use `ParseOperation` to `unset` the property:

```swift
func removeProfilePhoto(for userProfile: UserProfile) async throws -> UserProfile? {
    let operations = userProfile.operation
       .unset(("photo", \.photo))
    return try await operations.save()
}

```

---

<div class="post-metadata">

**Author:** ![jayson](https://community.parseplatform.org/user_avatar/community.parseplatform.org/jayson/32/679_2.png) [@jayson](https://community.parseplatform.org/u/jayson)\
**Post date:** [November 6, 2021, 2:22am UTC](https://community.parseplatform.org/t/deleting-setting-a-pointer-to-nil/2148/3 "2021-11-06T02:22:15Z")

</div>

ha.

The one place I forgot to check was the repo issues lol.

Thank you.

---

<div class="post-metadata">

**Author:** ![kulanu](https://community.parseplatform.org/user_avatar/community.parseplatform.org/kulanu/32/518_2.png) [@kulanu](https://community.parseplatform.org/u/kulanu)\
**Post date:** [November 11, 2021, 5:32pm UTC](https://community.parseplatform.org/t/deleting-setting-a-pointer-to-nil/2148/4 "2021-11-11T17:32:50Z")

</div>

It is the docs too:

[JS Guide](https://docs.parseplatform.org/js/guide/#destroying-objects)

[API Documentation](http://parseplatform.org/Parse-SDK-JS/api/3.4.0/Parse.Object.html#unset):

---

<div class="post-metadata">

**Author:** ![cbaker6](https://community.parseplatform.org/user_avatar/community.parseplatform.org/cbaker6/32/114_2.png) [@cbaker6](https://community.parseplatform.org/u/cbaker6)\
**Post date:** [November 11, 2021, 10:08pm UTC](https://community.parseplatform.org/t/deleting-setting-a-pointer-to-nil/2148/5 "2021-11-11T22:08:52Z")

</div>

This question was about the Swift SDK, the JS docs won’t help as they are for a different language.

---

<div class="post-metadata">

**Author:** ![LilMoke](https://community.parseplatform.org/user_avatar/community.parseplatform.org/lilmoke/32/855_2.png) [@LilMoke](https://community.parseplatform.org/u/LilMoke)\
**Post date:** [February 23, 2022, 10:13pm UTC](https://community.parseplatform.org/t/deleting-setting-a-pointer-to-nil/2148/6 "2022-02-23T22:13:43Z")

</div>

So how do you go about setting a pointer field?

---

<div class="post-metadata">

**Author:** ![cbaker6](https://community.parseplatform.org/user_avatar/community.parseplatform.org/cbaker6/32/114_2.png) [@cbaker6](https://community.parseplatform.org/u/cbaker6)\
**Post date:** [February 23, 2022, 10:28pm UTC](https://community.parseplatform.org/t/deleting-setting-a-pointer-to-nil/2148/7 "2022-02-23T22:28:54Z")

</div>

The playgrounds shows how to go about most of the basic functionality of the SDK:

> **[Parse-Swift/ParseSwift.playground/Pages at main · parse-community/Parse-Swift](https://github.com/parse-community/Parse-Swift/tree/main/ParseSwift.playground/Pages)**
>
> main/ParseSwift.playground/Pages

Specifically:

> <https://github.com/parse-community/Parse-Swift/blob/a34c5cb8257bc21a091cca56a59ce03eeb1ca927/ParseSwift.playground/Pages/8%20-%20Pointers.xcplaygroundpage/Contents.swift#L95-L109>

---

<div class="post-metadata">

**Author:** ![LilMoke](https://community.parseplatform.org/user_avatar/community.parseplatform.org/lilmoke/32/855_2.png) [@LilMoke](https://community.parseplatform.org/u/LilMoke)\
**Post date:** [February 23, 2022, 10:38pm UTC](https://community.parseplatform.org/t/deleting-setting-a-pointer-to-nil/2148/8 "2022-02-23T22:38:51Z")

</div>

Sorry I should have been more specific.

I have an object that contains a bool and a ParseFile like this:

```auto
struct ProfilePicture: ParseObject, Identifiable, Decodable
{
	// Mandatory properties
	var ACL: ParseACL?
	var createdAt: Date?
	var originalData: Data?
	var objectId: String?
	var updatedAt: Date?
	
	// Custom properties
	var userProfilePicture: ParseFile?
	var verified: Bool?
}

```

And I have a pointer in my user object like this:

```auto
struct User: ParseUser, ParseObject
{
	// Mandatory properties
	var ACL: ParseACL?
	var authData: [String: [String: String]?]?
	var createdAt: Date?
	var email: String?
	var emailVerified: Bool?
	var originalData: Data?
	var objectId: String?
	var password: String?
	var updatedAt: Date?
	var username: String?
	
	// Custom properties
	var firstName: String?
	var lastName: String?
	var matchInfo: MatchInfo?
	var profilePicture: ProfilePicture?
}

```

I learned from this post how to set the **userProfilePicture** field to nil using unset, but how to I go about assigning a new **ProfilePicture object** to the user field? I tried this code:

```auto
let imageData = unwrapImage.pngData()
let parseImageFile = ParseFile(data: imageData!)
var profilePicture = ProfilePicture()
profilePicture.verified = false
profilePicture.userProfilePicture = parseImageFile
profilePicture.save { _ in
	var currentUser = User.current
	currentUser?.profilePicture = profilePicture
	currentUser?.save { _ in
		User.current?.fetch(includeKeys: ["*"]) { result in
		}
	}
}

```

But it does not save the ProfilePicture object.

---

<div class="post-metadata">

**Author:** ![cbaker6](https://community.parseplatform.org/user_avatar/community.parseplatform.org/cbaker6/32/114_2.png) [@cbaker6](https://community.parseplatform.org/u/cbaker6)\
**Post date:** [February 23, 2022, 11:16pm UTC](https://community.parseplatform.org/t/deleting-setting-a-pointer-to-nil/2148/9 "2022-02-23T23:16:07Z")

</div>

> [@LilMoke](#):
>
> ```auto
> profilePicture.save { _ in
> var currentUser = User.current
> currentUser?.profilePicture = profilePicture
> currentUser?.save { _ in
> User.current?.fetch(includeKeys: ["*"]) { result in
> }
> }
> }
> 
> ```

It looks like you missed saving the returned object from the Parse Server, so your code is probably saving multiple objects because you are referencing an unsaved `profilePicture`:

```swift
// Using completion blocks...
profilePicture.save { result in
  switch result {
  case .success(let updatedProfile):
    var currentUser = User.current
    currentUser?.profilePicture = updatedProfile
    currentUser?.save { _ in
      User.current?.fetch(includeKeys: ["*"]) { _ in }
    }
  case .failure(let error):
     // handle error
  }
}

// Using async/await may make it easier to see...

do {
  let updatedProfile = try await profilePicture.save()
  var currentUser = User.current
  currentUser?.profilePicture = updatedProfile
  let updatedUser = try await currentUser?.save()
  let fetchedUser = try await updatedUser?.fetch()
  ...
} catch {
  // handle error
}

```
