Questions tagged [mutating-function]

33 questions
7
votes
2 answers

Swift : Enum case not found in type

I have been searching for many questions here, I found one with similar title Enum case switch not found in type, but no solution for me. I'd like to use enum with mutation of itself to solve question, what is the next traffic light color, at…
GiorgioE
  • 204
  • 2
  • 10
6
votes
2 answers

Mutating / in-place function invokation or adaptation in Python

I often have statements in my code that do the following: long_descriptive_variable_name = some_function(long_descriptive_variable_name) which is very clear, but verbose and somewhat redundant at the same time. Is there any way in Python to…
Amelio Vazquez-Reina
  • 74,000
  • 116
  • 321
  • 514
5
votes
4 answers

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.
subin272
  • 601
  • 5
  • 21
3
votes
1 answer

Swift mutating Function as first class value

I can have a function to swap the first two elements of an array: func swapFirstTwo(array: inout [Int]) { if array.count >= 2 { array.swapAt(0, 1) } } typealias Swapper = (inout [Int]) -> () // And I can have a variable = the function let…
Adahus
  • 1,465
  • 14
  • 25
3
votes
3 answers

Capturing a struct reference in a closure doesn't allow mutations to occur

I am trying to see if I can use structs for my model and was trying this. When I call vm.testClosure(), it does not change the value of x and I am not sure why. struct Model { var x = 10.0 } var m = Model() class ViewModel { let…
aland
  • 41
  • 1
  • 2
2
votes
3 answers

Mutating function inside structure

I'm using Swift 4, I have a structure that I initialize with default values. I made a function inside that is supposed to read a JSON and change those default values with what it gets but it doesn't seem to work. Error: Closure cannot implicitly…
Mika
  • 67
  • 8
2
votes
1 answer

Mutating Function in Protocol Extension Where Self is UIViewController

I've written a protocol and corresponding extension which utilizes a simple StringStack in tandem with a naming convention of the form "@" to perform segues between nested views in my Swift application. I'm new to swift so if…
2
votes
1 answer

Swift NSDate Extension Error: Mutating isn't valid on methods in classes or class-bound protocols

I am trying to extending NSDate but am getting two errors : extension NSDate { //'mutating' isn't valid on methods in classes or class-bound protocols mutating func addMonth() { let calendar = NSCalendar.currentCalendar() let…
Yannick
  • 2,954
  • 1
  • 17
  • 29
1
vote
1 answer

How to fill a list using Completion Handlers [SWIFT UI]

I want to fill a list using a completion handler, the problem is that it's loading nil in the first execution and marks errors when I try to consume my View where I have my list that it's filled by completion handler... Any suggestions to fix…
1
vote
3 answers

Swift struct mutating a variable not working?

I am not able to modify my model class variable even using mutating func keyword in a method? So basically I have wrapped my problem in a very easy way I have a class Car that has 3 variable id, start, and modelNo After that initialize an empty Car…
iamVishal16
  • 1,576
  • 17
  • 36
1
vote
2 answers

using Object.values() method to assign a constant array?

How would I fill const objArray with numObj object's values using the Object.values() method? I've only been able to do it with a for loop + push method (demonstrated below) const numObj = { oddNum: 1, evenNum: 2, foundNum: 5, randomNum:…
1
vote
1 answer

Why mutating function next does not change the struct (conforming to Sequence and IteratorProtocol) after the iteration?

I write a stack struct, and make it conforming to IteratorProtocol and Sequence protocol. The next function is mutating. So I suppose the iteration of the stack will mutate the struct. import Foundation struct Stack { var…
shoujs
  • 1,053
  • 1
  • 11
  • 22
1
vote
2 answers

How to set a class property using a mutating function?

The following class provides a mutating function to change its property: class Person { struct Location { var coordinate: CLLocationCoordinate2D! var city: String? mutating func…
Manuel
  • 11,849
  • 3
  • 39
  • 93
1
vote
2 answers

Mutating fun call swift syntax

I'm reading through the Swift 2.2 man and trying to understand a function in the mutating fun section here is the code struct TestPoint { var x = 0.0, y = 0.0 mutating func moveByX(deltaX: Double, y deltaY: Double) { self =…
0
votes
1 answer

Swift mutating function struct pass struct’s variable name in function?

I am working in Swift trying to update an organization struct that will need to hold a latitude and longitude. I created a mutating function in the struct that will update the latitude and longitude based on the organization organization struct’s…
1
2 3