[swift-users] Swift 4 "Cannot use mutating member on immutable value: 'self' is immutable"

Zhao Xin owenzx at gmail.com
Wed Sep 13 20:11:41 CDT 2017


Change `self` to `ModelFetcher`. You are calling a class static property,
not a class instance property.

Zhao Xin

On Thu, Sep 14, 2017 at 8:37 AM, Rick Mann via swift-users <
swift-users at swift.org> wrote:

> Moving to Swift 4, I'm running into an issue for which I can't seem to
> find an answer in google:
>
> "Cannot use mutating member on immutable value: 'self' is immutable"
>
> The code looks like:
>
> class
> ModelFetcher : NSObject, URLSessionDelegate
> {
>     ...
>     static  let     managerDispatchQueue                    =
>  DispatchQueue(label: "Model Download Manager Queue")
>     static  var     pendingFetchers                         =
>  [ModelFetcher]()
>     static  var     currentFetcher:         ModelFetcher?
>
>     class
>     func
>     startNextFetcher()
>     {
>         self.managerDispatchQueue.async
>         {
>             guard
>                 self.currentFetcher == nil,
>                 let mf = self.pendingFetchers.popFirst()
>                          ~~~~ ^                             error: cannot
> use mutating member on immutable value: 'self' is immutable
>             else
>             {
>                 return
>             }
>
>             self.currentFetcher = mf
>             mf.start()
>         }
>     }
>     ...
> }
>
> This code compiled fine in Xcode 8, or in Xcode 9/Swift 3.2 as a
> monolithic app (the error shows up when this code is factored into a
> framework). Other mutating references to self seem to compile okay (e.g.
> "self.currentFetcher = nil" or "self.pendingFetchers.remove(at: idx)").
> Not sure what's special about this one.
>
>
> --
> Rick Mann
> rmann at latencyzero.com
>
>
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170914/5de3dfa1/attachment.html>


More information about the swift-users mailing list