2
votes

I have this simple piece of Swift code:

let message = "This is a test"

if message.range(of:"test") != nil {
    let message = "Changed string"
}

print(message)

What I want to do: If string message contains the string test, change it to Changed string. Else keep it unchanged.

I'm getting the following warning:

Initialization of immutable value 'message' was never used; consider replacing with assignment to '_' or removing it

for line

let message = "Changed string"

Also, printed output is still This is a test instead of expected Changed string.

What am I doing wrong?

4
Don’t use let within the conditional block as it has already been declared. Also, it should be var anyway so you can change it.Chris
As @Chris said, it's variable scope. Using let inside the if statement means you are actually declaring a second variable, not replacing the value of the first.dfd
message.range(of:"test").map { _ in message = "Changed string" } ;D (given, naturally, that message is mutable: var message = "This is a test")dfrib

4 Answers

8
votes

If you want it to change it, you should be making it a variable and using the same variable.

var message = "This is a test"

if message.range(of:"test") != nil {
    message = "Changed string"
}

print(message)
6
votes

So, a few people have already given you reasonable, technically correct answers, but i'd like to explain:

  • declaring a value as let means that it is a constant i.e. immutable
  • you are getting the warning because instead of modifying the value as you intended, you are redeclaring it within the scope of the if statement. in order to declare a mutable variable, we use var i.e.:

var message = "This is a test"

  • a more appropriate API for this conditional might be contains, because it is locale-unaware and returns Bool, rather than having to convert an NSRange to Bool by testing against != nil

Given the above, the final code would be as follows:

var message = "This is a test"

if message.contains("test") {
    message = "Changed string"
}

print(message)

Hope that helps!

2
votes

Swift 4 Implementation

Here is the simplest way I could solve this problem.

var message1 = "This is a test"
var message2 = "This is a"

print(message1.contains("test") ? "Changed String" : message1) //"Changed String"
print(message2.contains("test") ? "Changed String" : message2) //"This is a"
2
votes

There are two variables called message in your code. One is the inner variable with "Changed string". The other, outer one has value "This is a test". So, if you want change the variable just declare it with var.

If your code was like:

let message = "This is a test"

if message.range(of:"test") != nil {
    let message = "Changed string"
    print (message)
}

print(message)

You'll get this output:

"Changed string" "This is a test"

This is because the inner variable didn't change the outer variable. In the inner you just declare and don't use it. The warning is there just to advise you that you have one unused variable.

Instead of this double let, you could declare:

var message = "This is a test"

if message.range(of:"test") != nil {
    message = "Changed string"
}

print(message)

as Rakesha Shastri answered.