Type Has No Subscript Members?
Asked Answered
B

4

11

I get the error "Type 'Ship' has no subscript members when I try to do:

var coor = ship[index]

I tried to do

var coor = ship?[index] as? Coordinate

But I get this error: "Cannot use optional chaining on non-optional value of type 'Ship'"

Here's my Ship class:

import Foundation

class Ship: NSObject, NSCoding {

    var shipCoors: [Coordinate]?

    var count: Int {
        var count = 0
        for _ in shipCoors! {
            count++
        }
        return count
    }

    init(shipCoors: [Coordinate]) {
        self.shipCoors = shipCoors
    }

    required init(coder decoder: NSCoder) {
        self.shipCoors = decoder.decodeObjectForKey("shipCoors") as? [Coordinate]
    }

    func encodeWithCoder(coder: NSCoder) {
        coder.encodeObject(shipCoors, forKey: "shipCoors")
    }
}

The Coordinate class is also of type NSObject, NSCoding, etc... The objects seem to be in the array when I load them (from NSUserDefaults)? How do I get them out?!

Birdwatcher answered 21/3, 2016 at 17:44 Comment(2)
You want ship.shipCoors?[index]Centaurus
Your count property getter can just be return shipCoors?.count ?? 0 btw.Centaurus
B
12

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.

Boomer answered 21/3, 2016 at 17:58 Comment(0)
I
9

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()

Independence answered 13/10, 2016 at 21:23 Comment(2)
thanks for posting this answer. One can be out of school for a long time and still make this error (it seems).Kano
my pleasure, i face such issues on a daily basis :-)Independence
N
2

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

Nightly answered 21/3, 2016 at 17:49 Comment(0)
H
0

For my code:

func takeN(_ numbers: Int...) -> [Int]{
    var intArray : [Int] = []
    for n in numbers{
        intArray.append[n]

    }
    return intArray
}

I was getting the following error:

error: type '(Int) -> ()' has no subscript members intArray.append[n]

It means the append function doesn't use subscripts or []!

changing the line of

intArray.append[n]

to:

intArray.append(n)

would resolve the entire issue!

Hopeh answered 24/9, 2017 at 2:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.