Questions tagged [mutating-function]

38 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
  • 91,494
  • 132
  • 359
  • 564
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
  • 733
  • 6
  • 24
3
votes
1 answer

TypeScript / Vue 3: Injecting mutating function causes TypeScript error "Object is of type 'unknown'"

I’m new in TypeScript and trying to use it with Vue 3 composition API and provide / inject. Let's say in parent component A I have something like this: // Parent component A import { provide, ref } from 'vue'; import ScoreType from…
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…
Cortado-J
  • 2,035
  • 2
  • 20
  • 32
3
votes
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,780
  • 18
  • 40
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
3
votes
2 answers

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

How to use mutating member on immutable value?

I got the solution but can't understand why it works. my struct is QuizBrain() whenever i try to use this code, i got an error: Cannot use mutating member on immutable value: function call returns immutable value btw the checkAnswer method is…
Jarvis
  • 53
  • 3
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
  • 10
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
  • 3,210
  • 1
  • 21
  • 30
1
vote
3 answers

Is this a good way of using Enum and static var in enum to manage state in Swift and SwiftUI?

I am wondering if the following use of Enum, static var, computed property and whatnot are good practices to manage states in Swift and SwiftUI. I'm learning Swift by myself and don't have anyone to review my code or senior to learn from. enum…
D.Park
  • 53
  • 5
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…
Antonio Labra
  • 1,694
  • 2
  • 12
  • 21
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,113
  • 1
  • 11
  • 24
1
2 3