PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, April 28, 2022

[FIXED] Why is there a warning saying "Immutable value..."?

 April 28, 2022     dictionary, swift, warnings     No comments   

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing