Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 761 Vote(s) - 3.48 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Protocol Extension, Mutating Function

#1
I am using swift 2.0, I have a protocol and an extension on the protocol to create a default implementation of a method, the code is as fallows:

protocol ColorImpressionableProtocol {

var lightAccentColor: UIColor? {get set}
var accentColor: UIColor? {get set}
var darkAccentColor: UIColor? {get set}
var specialTextColor: UIColor? {get set}

mutating func adoptColorsFromImpresion(impresion: ColorImpressionableProtocol?)
}

extension ColorImpressionableProtocol {

mutating func adoptColorsFromImpresion(impresion: ColorImpressionableProtocol?){
lightAccentColor = impresion?.lightAccentColor
accentColor = impresion?.accentColor
darkAccentColor = impresion?.darkAccentColor
specialTextColor = impresion?.specialTextColor
}
}
I am later on in my code trying to call this method and am getting an error that reads:

"cannot use mutating member on immutable value:'self' is immutable"

The code is as fallows:

init(impresion: ColorImpressionableProtocol?){
super.init(nibName: nil, bundle: nil)
adoptColorsFromImpresion(impresion)
}

The only thing I can think of is that 'Self' in this case is a protocol, not a class. However I have to be missing something to make this concept work, A default implementation of a method defined by a protocol that edits values also defined by the same protocol.

Thank you for your help and time :)
Reply

#2
You are adopting this protocol in a class so the self (which is reference type) is immutable. The compiler expects self to be mutable because of the mutable method declared in protocol. That's the reason you are getting this error.

The possible solutions are :

> 1) Implement a non mutating version of the method where the protocol
> being adopted. ie: implement the method in adopting class instead as a
> protocol extension.
>

class MyClass : ColorImpressionableProtocol {

func adoptColorsFromImpresion(impresion: ColorImpressionableProtocol?){
lightAccentColor = impresion?.lightAccentColor
accentColor = impresion?.accentColor
darkAccentColor = impresion?.darkAccentColor
specialTextColor = impresion?.specialTextColor
}
}

>
2) Make the protocol as class only protocol. This way we can remove the *mutating* keyword. It's the easiest solution but it can be only used in class.

To make protocol class only :

protocol MyProtocolName : AnyObject { }
OR
protocol MyProtocolName : class { }

>
> 3) Make sure only value types adopt this protocol.This may not be useful in
> all scenarios.

[Here][1] is the detailed explanation and solution for this case.

[1]:

[To see links please register here]

Reply

#3
If you intend to use the protocol only for classes then you can make
it a *class protocol* (and remove the `mutating` keyword):

protocol ColorImpressionableProtocol : class {

// ...

func adoptColorsFromImpresion(impresion: ColorImpressionableProtocol?)
}

Then

init(impresion: ColorImpressionableProtocol?){
super.init(nibName: nil, bundle: nil)
adoptColorsFromImpresion(impresion)
}

compiles without problems.


Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through