Reputation: 4769
I am working on writing a Fibonacci number finder in Swift. I am having an issue with using a variable to access an array, like the following:
var i = 0;
var fibnumbers: Array = []
var lengthOfFibnumbers: Int = fibnumbers.count
var e: Int = lengthOfFibnumbers - 1
var addone: Int = fibnumbers[e]
When I try this, the playground shows me the red octagon with an exclamation point. When I click on it, it gives the following error: Could not find an overload for 'subscript' that accepts the supplied arguments
.
How can I fix this?
Upvotes: 0
Views: 2846
Reputation: 94773
You should declare fibnumbers as an array of Int:
var fibnumbers: [Int] = []
"Array" is defined as a generic (Array). When you don't provide a type for "Array" it is assumed to be "AnyObject". The error you are getting is a little weird but really it is because you are trying to assign the result of the subscript to an Int
when you need to convert it from an AnyObject
to an Int
. For example, this compiles fine: var addone : AnyObject = fibnumbers[e]
So it isn't really the subscript that is the problem, despite what the error says.
Upvotes: 4