2

In .playground I am just exploring Swift and I was attempting to create an array in an odd way, well, just because.

So this doesn't create an array of items 0, 1, 2,... 50, like I was naively hoping:

let numberArray = [0...50]

and trying to iterate through it:

for num in numberArray {
    println("The number is \(num)")
} 

gives this console output:

The number is VSs5Range (has 2 children)

What does that mean? Running this in a .swift file, the build fails so the console output doesn't give me anything.

Christian
  • 336
  • 1
  • 4
  • 16

3 Answers3

5

What's happening is that you are creating an array with [0...50], but the array has one element: the object 0...50, which is a new construct in Swift denoting a range (and apparently of the type VSs5Range.

So your loop over the initial array is displaying the single array element, which writes itself as the type name and the number of end points it has.

The range is not an array, it's something between a generator (iterators in C#) and a testing unit. You can use it in switch statements for example (case 0..3:) as well as generate numbers from it (for i in 0..3 {}). Personally I find it pretty cool!

Blindy
  • 65,249
  • 10
  • 91
  • 131
  • Ah, okay. So this mystical VSs5Range is a type somewhere within Swift? My google search of that term resulted in just two results - neither of them to do with Swift. And yes, I also tried iterating over a range with (for num in 0...50) and it is indeed very cool! – Christian Jun 06 '14 at 17:24
  • VSs5Range is the mangled name of a `Range`. Internally a lot of Swift stuff has names starting with three letters followed by one or more identifiers. 5Range is just Range with the length prepended. You may also see stuff like 3foo3bar7bazinga which have a composite name with three identifiers. – Analog File Jun 06 '14 at 17:41
  • @AnalogFile okay thanks. Do you know how I can print the type of a variable? Edit: pressed enter too soon. If I try className on it, I get nothing. – Christian Jun 06 '14 at 17:46
1

You created an array with a single object (which is "the range").

What you see is the internal representation of the range object, which has a weird undocumented class (similar to the NSArray subclasses you sometimes see.)

As Michael's answer shows, you don't necessarily need to create an array. You can do many things (like iterate) directly with the range object.

nschum
  • 15,322
  • 5
  • 58
  • 56
  • Thanks! Yep, I had already played around with iterating over a range which is really neat. Then I happened upon this and was just really confused. Thanks again! – Christian Jun 06 '14 at 17:26
0

If you do this instead:

let numberArray = 0...50

for num in numberArray {
    println("The number is \(num)")
}

You'll probably see a happier result.

I haven't gotten far enough into the Swift book yet, but I suspect brackets are only for specific, individual objects... while using the "..." and ".." range specifiers will create your array for you, without the need for brackets.

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • That doesn't really create an array. Try `let numberArray: Array = 0...50` and you'll get a compile error. But as you see you can iterate over ranges, too. – nschum Jun 06 '14 at 17:17
  • Thanks, @MichaelDautermann. That does work but as nschum mentioned, it's not an array. Which I suppose was part of my confusion in this thing as well. – Christian Jun 06 '14 at 17:30
  • @nschum is there a way to log what type of variable you have? Say I have a variable set to an array, can I find out or print to the console something that says "this is an array" – Christian Jun 06 '14 at 17:32
  • http://stackoverflow.com/questions/24006165/how-do-i-print-the-type-or-class-of-a-variable-in-swift – nschum Jun 06 '14 at 17:51