Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

413
Vistas
Is there any difference between "mutating" function and "inout" parameters in Swift?

As per Swift documentation both mutating and inout keywords are used to modify the value types from within a function. Is there any difference between "mutating" and "inout" and any special case where we need to use either of them.

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

mutating marks a method. inout marks a parameter. They are completely different things.

Methods marked with mutating can mutate self i.e. set properties of self, reassign self etc.

struct Foo {
    var foo: Int

    mutating func mutate() {
        foo += 1 // this is mutating self
    }
}

Parameters marked with inout basically become var variables, as opposed to let constants. You can change them, and the changes will also reflect on the caller's side.

func f(_ x: inout Int) {
    x = 10
}

var a = 1
f(&a)
print(a) // 10
over 4 years ago · Santiago Trujillo Denunciar

0

For me I see difference just in places where they are used.

Parameter marked with inout keyword allows you to pass your value to any method similary as reference

func square(inout num: Int) { 
    num = num * num
}

In contrast to it, method marked with mutating keyword is used in type scope and allows you to change value itself from this method

extension Int {

    mutating func square() {
        self = self * self
    }

}
over 4 years ago · Santiago Trujillo Denunciar

0

Value types' instance methods cannot change the value type's properties (or value itself), unless they are marked as mutating.

Functions of any kind cannot change their parameters (and have the change propagate outside the function) unless those parameters are marked as inout.

They do a similar job, but in different contexts.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda