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…
![](../../users/profiles/6790625.webp)
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…
![](../../users/profiles/283296.webp)
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.
![](../../users/profiles/4361846.webp)
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…
![](../../users/profiles/2335363.webp)
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…
![](../../users/profiles/5180675.webp)
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…
![](../../users/profiles/9635671.webp)
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…
![](../../users/profiles/3159288.webp)
rmorshea
- 670
- 1
- 5
- 19
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…
![](../../users/profiles/2902689.webp)
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…
![](../../users/profiles/9008966.webp)
Antonio Labra
- 459
- 3
- 13
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…
![](../../users/profiles/3423179.webp)
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:…
![](../../users/profiles/10048094.webp)
Needto Readwatch
- 11
- 1
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…
![](../../users/profiles/1455150.webp)
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…
![](../../users/profiles/1870795.webp)
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 =…
![](../../users/profiles/6592320.webp)
Puzzled Penguin
- 13
- 3
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…
![](../../users/profiles/15881225.webp)
Drew
- 1