redgenieuk
redgenieuk

Reputation: 145

Type 'SNstorelocation' has no subscript members - I just don't understand

I have been happily programming in Swift for a few weeks - really enjoying it, but I have hit a block. I have googled and I cannot find an explanation. I set up a much simpler test case, which didn't have the same problem.

I am getting the error "Type 'SNstorelocation' has no subscript members", and also "Value of type '[SNstorelocation]?' has no member 'append'".

I have read plenty about subscripting, but I don't think that is the problem. I want to have an array of structs, one of the elements is also an array of structs. I have seen it everywhere and my little test case it worked no problem.

So I have concluded (maybe incorrectly!) that somehow I haven't created and array. If I had, it should have an index to access the data and the code should work, but it doesn't.

Here is the little example I created, which I see as the same as my example, but simpler, which works.

struct test1 { var t1Item1: Int? var t1Item2: String? var myArray = test2 } struct test2 { var t2Item1: Int? var t2Item2: Int?

    init(v1: Int, v2: Int) {
        self.t2Item1 = v1
        self.t2Item2 = v2
    }
}

and I can do all the normal array things:

var myVar = [test1]()
var newItem = test1()
newItem.t1Item1 = 1
newItem.t1Item2 = "Hi"
myVar.append(newItem)
let myCount = myVar[0].myArray.count

Where as my example (just a few elements from the struct removed to keep it simple and short)

struct SNStoreItemGroups: Codable {
    var Welland: [SNStoreItem]
    var Other: [SNStoreItem]

    init() {
        self.Welland = []
        self.Other = []
    }
}
struct SNStoreItem: Codable {

    var locations =  [SNstorelocation]()
    var customerName: String?
    var customerPhone: String?
    var customerEmail: String?
    var notes: String?


}
struct SNstorelocation: Codable {
    let longitude: Double
    let latitude: Double
    let country: String
    let user: Int
    let timestamp: Double = Date().timeIntervalSinceReferenceDate
    init(_ lon: Double, _ lat: Double, _ ISOcountry: String, _ UserID: Int) {
        self.longitude = lon
        self.latitude = lat
        self.country = ISOcountry
        self.user = UserID
    }
}

var mySNData: SNStoreItem?
var SNStore: SNStoreItemGroups = SNStoreItemGroups()
// Some code to populate the SNStore



if let locIndex = SNStore.Welland[index].locations.index(where: { $0.longitude == MyLongitude }) {
    // This then causes the error
    // "Type 'SNstorelocation' has no subscript members"
                        if SNStore.Welland[index].locations[locIndex].timestamp {

                        }
                        }

Could someone please explain why this second example has no subscripts and the first one works OK? I just don't understand - especially because of the first let which seems to be OK in finding the index - I am sure I have just done something stupid!

TIA.

Upvotes: 0

Views: 54

Answers (1)

Warren Burton
Warren Burton

Reputation: 17372

Ambiguous error reporting by the Swift compiler…

A timestamp is not a Boolean so you need to compare the timestamp with something.

if let locIndex = mySNStore.welland[index].locations.index(where: { $0.longitude == 0.1234 }) {

    if mySNStore.welland[index].locations[locIndex].timestamp == Double(42) {

    }

}

You can see the correct error and the problem with:

let location = mySNStore.welland[index].locations[locIndex]
if location.timestamp {

}

You could raise a bug with the Swift team on this.

And as a style point Capitalised variables are horrible to read because they look like class names.

Upvotes: 1

Related Questions