Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inout parameters in swift 3

Tags:

ios

swift

swift3

This is my code:

func removeFromString( _ s: inout String, Character c:Character) -> Int {
    var s1 = s
    var nRemoved = 0

    while let ix = s1.characters.index(of: c) {
        s1.removeSubrange(ix...ix)
        nRemoved += 1

    }
    return nRemoved
}

var s2 = "everest"

let resb = removeFromString(&s2, Character: Character("e"))

My problem is this is supposed to change s2 to "vrst" after using the inout parameter s. But it's not modifying the var s2 at all. What wrong I'm I doing? Or I'm expecting a wrong output? Do help. Thanks.

like image 830
Mtoklitz113 Avatar asked Jun 28 '16 15:06

Mtoklitz113


People also ask

What is Inout parameter in Swift?

Swift inout parameter is a parameter that can be changed inside the function where it's passed into. To accept inout parameters, use the inout keyword in front of an argument. To pass a variable as an inout parameter, use the & operator in front of the parameter.

What is an Inout variable?

An InOut variable in a function header is for a variable that may be written as well as read within the function, but may also be written, as well as read outside the function.

What is in out inout parameters?

Procedure with IN-OUT parameter: An INOUT parameter is a combination of IN and OUT parameters. It means that the calling program may pass the argument, and the stored procedure can modify the INOUT parameter and pass the new value back to the calling program.

Can we use out and inout parameter in function?

A function can have OUT or IN OUT parameters, but this is bad coding practice. A function should have a return value and no out parameter. If you need more than one value from a function you should use a procedure.

What is an inout parameter in Swift?

An inout parameter is a value that is supplied into a function, updated by the function, and then returned out to replace the original value. What does _: mean in Swift? The underscore operator (_) in Swift denotes an unnamed argument or label. What is a good use case for an inout parameter in Swift?

How to modify passed-in parameters in Swift 3+?

A good use case will be swap function that it will modify the passed-in parameters. Swift 3+ Note: Starting in Swift 3, the inout keyword must come after the colon and before the type. For example, Swift 3+ now requires func changeChar (char: inout Character). Show activity on this post.

What are function parameters and return values in Swift?

Claim Discount In this tutorial, we will learn about function parameters and return values in Swift with the help of examples. A function parameter is a value that is accepted by a function. Before you learn about function parameters and return values, make sure to know about Swift functions.

What is an inout parameter in Python?

An inout parameter is a special type of parameter that can be modified inside a function and the changes apply outside the function. Here is an illustration of a function that takes a number A as an inout parameter and modifies it: To turn a function parameter into an inout variable that can be changed inside a function:


1 Answers

By moving s into s1 you are making a copy of it since String is a struct, and all structs are pass by value. So in the end you're only changing s1 and not the passed in string, s. Just remove it:

func removeFromString( _ s: inout String, Character c:Character) -> Int {
    var nRemoved = 0

    while let ix = s.characters.index(of: c) {
        s.removeSubrange(ix...ix)
        nRemoved += 1

    }
    return nRemoved
}
like image 107
Luke Avatar answered Oct 05 '22 22:10

Luke