0

I need to populate an Array (already declared and initialized) using a for loop in order to create a determinate amount of items.

I ended up with the following code:

    func createValues() -> Array<Int> {

        let usableRange:Range = 6..<11;

        var arrayOfValues: Array<Int>=[]; //Array declared and initialized

        for i in 0..<10 {

            arrayOfValues.append(random(usableRange));

            print(arrayOfValues[i]);

        }

        return arrayOfValues;
    }

this code does what I expect it to do just fine. However, as soon as I comment out the line

 print(arrayOfValues[i]);

Xcode throws the following warning:

Immutable value 'i' was never used; consider replacing with '_' or removing it

If I accept the suggestion the code works, but not as fine as it did before.

I'm just transitioning from Obj-C to Swift and I don't really know what the proper way to do this should be. Any help would be appreciated. Thanks in advance.

P.S. I'm aware that I don't need semicolons anymore, but old habits die hard, I guess...

8
  • 2
    "not as fine as it did before." Why not? The only difference is that it doesn't print anything Commented Apr 29, 2018 at 11:49
  • Please learn to read and understand error / warning messages. This is one of the most comprehensible. Commented Apr 29, 2018 at 11:52
  • well, by the press of a button, the code should generate random numbers between a range. If the average of the given numbers is not the average of the range it generates new numbers until it does. This is all fine, but when I replace the 'i' for a _, I have to press the button many times until I have something printed on the console. I'm debugging it right now to see where the problem can be... Commented Apr 29, 2018 at 11:53
  • @vadian I understand the warning just fine. The question is how to do what I want to do in a proper warning-free way Commented Apr 29, 2018 at 11:55
  • As Leo already mentioned the code does exactly the same (creating 10 random numbers) except it does not print anything in the loop. Commented Apr 29, 2018 at 11:56

4 Answers 4

1

Since you don't use i, you can just write

for _ in 0 ..< 10

The _ means "yes, there is a value, but I don't care about it", here and in many other situations.

Sign up to request clarification or add additional context in comments.

Comments

0

If you want just a good alternative for your code, I'm offering you this:

var i: Int = 0

while i < 10 {

  arrayOfValues.append(random(usableRange))
  i += 1

}

Comments

0

If the goal is to generate an array of random numbers in your given range, I would suggest you simply generate it directly. There is no need for the for loop.

let usableRange = UInt32(6)..<UInt32(11)
let arr = (0..<10).map { _ in Int(
    arc4random_uniform(usableRange.upperBound - usableRange.lowerBound) 
    + usableRange.lowerBound
)}

Comments

0

Array has a designated initalizer, which initalizes an array with a given size and a repeated value:

let values = Array(repeating: "VALUE", count: 5)
print(fiveZs)
// Prints "["VALUE", "VALUE", "VALUE", "VALUE", "VALUE"]"

Source: Apple Documentation

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.