Type Has No Subscript Members?
You have to explicitly add subscripting support to the class in order to use the subscript syntax, e.g. ship[index]
.
Here are the docs that cover subscripting and how to add subscripting to your class:
https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Subscripts.html
Add a subscript to your Ship
object to return an optional Coordinate
:
subscript(index: Int) -> Coordinate? {
guard let coordinate = shipCoors?[index] else {
return nil
}
return coordinate
}
shipCoors
is declared as [Coordinate]?
(an optional array), so there's a risk a Ship
won't have an array in shipCoors
. In this case I return nil
, but you can return whatever you want instead.
Had similar issue in Swift 3
Type '() -> [myObject]' has no subscript members
In my case, It was a simple case of not adding the brackets to the function-call "()". school boy error.
i.e. the below code was the culprit
dataModel.myFunction
Solved with dataModel.myFunction()