类型 'SNstorelocation' 没有下标成员 - 我只是不明白
Type 'SNstorelocation' has no subscript members - I just don't understand
我在 Swift 中愉快地编程了几个星期 - 真的很喜欢它,但我遇到了障碍。我用谷歌搜索,找不到解释。我设置了一个更简单的测试用例,它没有出现同样的问题。
我收到错误 "Type 'SNstorelocation' has no subscript members",还有 "Value of type '[SNstorelocation]?' has no member 'append'"。
我已经阅读了很多关于下标的内容,但我认为这不是问题所在。我想要一个结构数组,其中一个元素也是一个结构数组。我到处都看到它,我的小测试用例也没有问题。
所以我得出结论(也许是错误的!)我没有创建和排列。如果我有,它应该有一个索引来访问数据并且代码应该可以工作,但它没有。
这是我创建的小示例,我认为它与我的示例相同,但更简单,有效。
结构测试1 {
var t1Item1:整数?
var t1Item2:字符串?
var myArray = 测试 2
}
结构测试2 {
var t2Item1:整数?
var t2Item2: Int?
init(v1: Int, v2: Int) {
self.t2Item1 = v1
self.t2Item2 = v2
}
}
而且我可以做所有普通数组的事情:
var myVar = [test1]()
var newItem = test1()
newItem.t1Item1 = 1
newItem.t1Item2 = "Hi"
myVar.append(newItem)
let myCount = myVar[0].myArray.count
在我的示例中(仅从结构中删除了几个元素以保持简单和简短)
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: { [=13=].longitude == MyLongitude }) {
// This then causes the error
// "Type 'SNstorelocation' has no subscript members"
if SNStore.Welland[index].locations[locIndex].timestamp {
}
}
有人能解释一下为什么第二个例子没有下标而第一个例子没问题吗?我只是不明白 - 特别是因为第一个 let 似乎可以找到索引 - 我确定我刚刚做了一些愚蠢的事情!
TIA。
Swift 编译器报告不明确的错误…
A timestamp
不是布尔值,因此您需要将时间戳与某些内容进行比较。
if let locIndex = mySNStore.welland[index].locations.index(where: { [=10=].longitude == 0.1234 }) {
if mySNStore.welland[index].locations[locIndex].timestamp == Double(42) {
}
}
您可以通过以下方式查看正确的错误和问题:
let location = mySNStore.welland[index].locations[locIndex]
if location.timestamp {
}
您可以就此向 Swift 团队提出错误。
作为风格点,大写的变量很难读,因为它们看起来像 class 名字。
我在 Swift 中愉快地编程了几个星期 - 真的很喜欢它,但我遇到了障碍。我用谷歌搜索,找不到解释。我设置了一个更简单的测试用例,它没有出现同样的问题。
我收到错误 "Type 'SNstorelocation' has no subscript members",还有 "Value of type '[SNstorelocation]?' has no member 'append'"。
我已经阅读了很多关于下标的内容,但我认为这不是问题所在。我想要一个结构数组,其中一个元素也是一个结构数组。我到处都看到它,我的小测试用例也没有问题。
所以我得出结论(也许是错误的!)我没有创建和排列。如果我有,它应该有一个索引来访问数据并且代码应该可以工作,但它没有。
这是我创建的小示例,我认为它与我的示例相同,但更简单,有效。
结构测试1 { var t1Item1:整数? var t1Item2:字符串? var myArray = 测试 2 } 结构测试2 { var t2Item1:整数? var t2Item2: Int?
init(v1: Int, v2: Int) {
self.t2Item1 = v1
self.t2Item2 = v2
}
}
而且我可以做所有普通数组的事情:
var myVar = [test1]()
var newItem = test1()
newItem.t1Item1 = 1
newItem.t1Item2 = "Hi"
myVar.append(newItem)
let myCount = myVar[0].myArray.count
在我的示例中(仅从结构中删除了几个元素以保持简单和简短)
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: { [=13=].longitude == MyLongitude }) {
// This then causes the error
// "Type 'SNstorelocation' has no subscript members"
if SNStore.Welland[index].locations[locIndex].timestamp {
}
}
有人能解释一下为什么第二个例子没有下标而第一个例子没问题吗?我只是不明白 - 特别是因为第一个 let 似乎可以找到索引 - 我确定我刚刚做了一些愚蠢的事情!
TIA。
Swift 编译器报告不明确的错误…
A timestamp
不是布尔值,因此您需要将时间戳与某些内容进行比较。
if let locIndex = mySNStore.welland[index].locations.index(where: { [=10=].longitude == 0.1234 }) {
if mySNStore.welland[index].locations[locIndex].timestamp == Double(42) {
}
}
您可以通过以下方式查看正确的错误和问题:
let location = mySNStore.welland[index].locations[locIndex]
if location.timestamp {
}
您可以就此向 Swift 团队提出错误。
作为风格点,大写的变量很难读,因为它们看起来像 class 名字。