Hello I am trying to learn swift. I have a little experience with javascript so i tried modeling this loop in the same manner i usually do. The function actually outputs what its supposed to but I keep getting an error message and I am unsure of what I am doing wrong. Here is my code:
import UIKit
let dir: [String] = ["north", "east", "south", "west"]
var num = dir.count
func move(){
for i in 0 ... num{
var holder = dir[i]
switch holder{
case "north":
print("you've moved north")
case "east":
print("you've moved east")
case "south":
print("you've moved south")
case "west":
print("you've moved west")
default:
print("where you going?")
}
if i == 3{
print("round the world")
}
}
}
move()
i get this error on the last line "move()"
error: Execution was interrupted, reason: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0).
this is what outputs to the console:
you've moved north
you've moved east
you've moved south
you've moved west
round the world
Fatal error: Index out of range: file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-1103.2.25.8/swift/stdlib/public/core/ContiguousArrayBuffer.swift, line 444
