# AnyObject parameter: "Cannot assign to immutable expression"

**URL:** <https://forums.swift.org/t/anyobject-parameter-cannot-assign-to-immutable-expression/16642>\
**Category:** Using Swift\
**Created:** [October 3, 2018, 12:45am UTC](https://forums.swift.org/t/anyobject-parameter-cannot-assign-to-immutable-expression/16642 "2018-10-03T00:45:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pkamb](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/pkamb/32/2500_2.png) [@pkamb](https://forums.swift.org/u/pkamb)\
**Post date:** [October 3, 2018, 12:45am UTC](https://forums.swift.org/t/anyobject-parameter-cannot-assign-to-immutable-expression/16642/1 "2018-10-03T00:45:58Z")

</div>

I'm hitting this issue in my `NSOpenSavePanelDelegate` in a Sandboxed Mac app.

[https://developer.apple.com/documentation/appkit/nsopensavepaneldelegate/1527117-panel](https://developer.apple.com/documentation/appkit/nsopensavepaneldelegate/1527117-panel)

I need to get a reference to the sender as a Panel and set the directory.

```swift
 /* NSSavePanel/NSOpenPanel: Gets and sets the directoryURL shown. */
 @available(OSX 10.6, *)
 open var directoryURL: URL?

```

All of my `as?` casts fail because the Sandbox uses a private subclass for these items.

If I use AnyObject, Xcode autocomplete suggests the correct method as suggested here:

> <https://stackoverflow.com/questions/37119631/swift-os-x-sandboxing-treat-nsvbopenpanel-as-a-nsopenpanel-because-i-n/37120795#37120795>

But for some reason the variable is marked as immutable, and thus I cannot actually set any properties...?

```
extension PanelController: NSOpenSavePanelDelegate {
    
    func panel(_ sender: Any, didChangeToDirectoryURL url: URL?) {
        
        // Sandboxed app; PowerBox open/save panels
        // sender is `NSVBOpenPanel` and/or `NSKVONotifying_NSVBOpenPanel`
        // but I cannot `as?` cast to those private classes
        
        var panel = sender as AnyObject
        
        // ERROR: "Cannot assign to immutable expression of type 'URL??'"
        panel.directoryURL = URL(fileURLWithPath: "/")!

        // ERROR: "Cannot assign to immutable expression of type 'URL?'"
        panel.directoryURL? = URL(fileURLWithPath: "/")!
    }
}

```

Any suggestions for getting past this error or getting a reference to the Panel?

---

<div class="post-metadata">

**Author:** ![jrose](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jrose/32/408_2.png) [@jrose](https://forums.swift.org/u/jrose)\
**Post date:** [October 3, 2018, 1:02am UTC](https://forums.swift.org/t/anyobject-parameter-cannot-assign-to-immutable-expression/16642/2 "2018-10-03T01:02:48Z")

</div>

This is a longstanding bug, but it looks like we don't have a JIRA bug for it. Inside Apple it's \<rdar://problem/15233922\> "Cannot assign to a property through AnyObject", and it's very similar to [SR-5475](https://bugs.swift.org/browse/SR-5475) (which is about `optional` properties of protocols). I don't think we have a good workaround other than defining a little `static inline` function in Objective-C that will do the call for you without trying to check anything.

```objectivec
NS_SWIFT_NAME(setDirectoryURL(_:for:))
static inline void setDirectoryURLForPanel(id panel, NSURL * _Nonnull url) {
  NSSavePanel *realPanel = (NSSavePanel *)panel;
  realPanel.directoryURL = url;
}

```

An alternate approach in this specific case would be to file a bug against Apple that the `sender` here is not an instance of NSOpenPanel or at least NSSavePanel.

---

<div class="post-metadata">

**Author:** ![pkamb](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/pkamb/32/2500_2.png) [@pkamb](https://forums.swift.org/u/pkamb)\
**Post date:** [October 3, 2018, 4:29am UTC](https://forums.swift.org/t/anyobject-parameter-cannot-assign-to-immutable-expression/16642/3 "2018-10-03T04:29:04Z")

</div>

Thank you for the quick reply. I'll try that workaround. I opened a JIRA bug here:

Cannot assign to a property through AnyObject

> <https://github.com/apple/swift/issues/51406>
>
> | | |
> |------------------|-----------------|…
> |Previous ID | SR-8900 |
> |Radar | rdar://problem/15233922 |
> |Original Reporter | pkamb (JIRA User) |
> |Type | Bug |
> 
> \<details\>
> \<summary\>Environment\</summary\>
> 
> Swift 4.2, Xcode 10
> 
> 
> \</details\>
> 
> \<details\>
> \<summary\>Additional Detail from JIRA\</summary\>
> 
> | | |
> |------------------|-----------------|
> |Votes | 0 |
> |Component/s | Compiler |
> |Labels | Bug |
> |Assignee | None |
> |Priority | Medium |
> 
>    
> 
> md5: fecd1beb40b55f31a3a7e32235cd1f19
> 
> \</details\>
> 
> 
> 
> 
> \*\*relates to\*\*:  
> \* \[SR-5475\](https://bugs.swift.org/browse/SR-5475) Can't set property declared optional in protocol  
> 
> 
> \*\*Issue Description:\*\*
> 
> 
> Public bug for \\\<rdar://problem/15233922\\\> "Cannot assign to a property through AnyObject"
> 
> I'm attempting to implement NSOpenSavePanelDelegate in a Sandboxed Mac app.
> 
> The \\\`sender: Any\\\` parameter is a private system class that I cannot cast to. The private classes do not inherit from the public alternatives, so I cannot cast to those either.
> 
> I'm forced to use AnyObject. But doing so results in the error \\\`Cannot assign to immutable expression\\\`.
> 
> Sample Code:
> 
> \`\`\` swift
> extension PanelController: NSOpenSavePanelDelegate {
>     
> func panel(\_ sender: Any, didChangeToDirectoryURL url: URL?) {
> switch sender {
> case is NSSavePanel: () // false
> case is NSOpenPanel: () // false
> default:
> print("Sandboxed app. Uses private PowerBox open/save panel classes such as NSVBOpenPanel")
> }
>         
> var panel = sender as AnyObject
>         
> // Error: Cannot assign to immutable expression of type 'URL?'
> panel.directoryURL? = URL(fileURLWithPath: "/")!
> }
> 
> }
> \`\`\`
> 
> Also posted in the Swift Forums here:  
> \<https://forums.swift.org/t/anyobject-parameter-cannot-assign-to-immutable-expression/16642\>

---

<div class="post-metadata">

**Author:** ![eskimo](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/eskimo/32/13558_2.png) [@eskimo](https://forums.swift.org/u/eskimo)\
**Post date:** [October 3, 2018, 1:50pm UTC](https://forums.swift.org/t/anyobject-parameter-cannot-assign-to-immutable-expression/16642/4 "2018-10-03T13:50:22Z")

</div>

> Any suggestions for … getting a reference to the Panel?

By way of workarounds, it seems like the `PanelController` class is intended to control a panel. In that case I’d store a reference to that panel in an instance property. That way my delegate could access it directly, rather than go through the `sender` parameter.

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple

---

<div class="post-metadata">

**Author:** ![pkamb](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/pkamb/32/2500_2.png) [@pkamb](https://forums.swift.org/u/pkamb)\
**Post date:** [October 3, 2018, 6:09pm UTC](https://forums.swift.org/t/anyobject-parameter-cannot-assign-to-immutable-expression/16642/5 "2018-10-03T18:09:10Z")

</div>

Ok, that does work and is probably the best workaround:

```
if sender as AnyObject === myOpenPanel as AnyObject {
    myOpenPanel?.directoryURL = URL(fileURLWithPath: "/")
}

```

Now I find that setting the directoryURL does not seem to work within the Sandbox! Ugh. I guess there is then no way to limit the OpenPanel to a single directory.

---

<div class="post-metadata">

**Author:** ![eskimo](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/eskimo/32/13558_2.png) [@eskimo](https://forums.swift.org/u/eskimo)\
**Post date:** [October 4, 2018, 4:10pm UTC](https://forums.swift.org/t/anyobject-parameter-cannot-assign-to-immutable-expression/16642/6 "2018-10-04T16:10:53Z")

</div>

> I guess there is then no way to limit the OpenPanel to a single  
> directory.

Alas, I don’t know the answer to that one off the top of my head. You could ask over on [DevForums](https://forums.developer.apple.com/community/distribution/mac-app-sandbox) to see if anyone’s done that before. If that doesn’t work out, you should open a [DTS tech support incident](https://developer.apple.com/support/technical/) and speak to our App Sandbox expert.

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple
