Issue
I keep getting a warning saying "Immutable value 'key' was never used" in Xcode when I run my program in Swift, specifically at my for loop that prints out all the values of a dictionary. I've searched online for alternative ways to print these out without getting warned, but I haven't been able to find anything.
Here is the section of the program where the warning appears. It is at for (key, value)
.
var dict = [String : [String]]()
dict["key1"] = ["Bruh", "Bruhh", "Bruhhh", "Bruhhhh"]
dict["key1"]!.append("!")
for (key,value) in dict
{
print("\(value)")
}
print("\n")
I've seen code before with numerous warnings, but the programmers seem to be fine with them. Should I just ignore this issue?
Solution
If you're not gonna use key
, you can silence the warning be replacing key
with _
So it'd look like this:
var dict = [String : [String]]()
dict["key1"] = ["Bruh", "Bruhh", "Bruhhh", "Bruhhhh"]
dict["key1"]!.append("!")
for (_, value) in dict
{
print("\(value)")
}
print("\n")
This works in other places in Swift too:
func example() -> Int {
return 1
}
let a = example() //Initialization of immutable value 'a' was never used
let _ = example() //No warning
Answered By - Will Answer Checked By - Senaida (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.