Swift 结构没有看到它的内容?

Swift 结构没有看到它的内容?,swift,struct,Swift,Struct,所以我想得到数据 struct Location: Decodable { var elevation = String() var id = String() var latitude = String() var longitude = String() var name = String() var region = String() var unitaryAuthArea = String() } struct Lo

所以我想得到数据

struct  Location: Decodable {
    var elevation = String()
    var id = String()
    var latitude = String()
    var longitude = String()
    var name = String()
    var region = String()
    var unitaryAuthArea = String()
     }

struct  Locations: Decodable {
    var Location :[Location]
}


struct Weather: Decodable{
    var Locations :[Locations]
}
然而,由于某种原因,当我使用这一行时

  let withoutElevation = weather.Locations.Location.filter {$0.elevation == nil}

我无法得到位置没有位置的错误。你知道为什么吗

通过检查在“链”的每个阶段起作用的类型,可以更好地理解这一点

在这一点上,错误变得很明显
weather.Locations
是一个
[Locations]
,它没有
Location
成员。您实际想要的是获取
位置
结构中的所有
位置
s

知道这一点,再尝试一次,看看你能走多远。如果您还有其他问题,请告诉我


请注意:这是一个很好的学习机会。我试着给奥普指出正确的方向,希望他能从那里进步。请不要马上破坏答案

如果您遵循变量名称应以小写字母开头的标准,您的编码体验将得到改善。结构/类名以大写字母开头。因此,
var Locations:[Locations]
行应该是
var Locations:[Locations]
。其他人也一样。请注意:这是一个很好的学习机会。我试着给奥普指出正确的方向,希望他能从那里进步。请不要把答案弄糟了away@DamianTalaga默认情况下,您的
位置的高程为
的纬度/长度等,并且所有这些都是可变的(位置的纬度和长度可以更改?这很奇怪)。那没什么意义,是吗?您可以使用更合适的数据类型更好地对此进行建模(例如,对于
lat
/
long
/
elevation
),缩小易变性(
let
而不是
var
),并且不为所有值提供非敏感的默认值(不要将所有值都默认为
String()
)@亚历山大:你应该把那句话写在你的答案上。@rmaddy完成了。有时候冗余是件好事,因为冗余有时候是好的。谢谢,这正是我所想的,我只是不知道为什么在我指定位置之前,位置有位置,所以不知道出了什么问题there@DamianTalaga对不起,我完全不理解你的评论。你能详细说明一下吗?我已经把位置作为一个对象放在里面了,所以我不知道为什么它不是seen@DamianTalaga
Locations
确实有
Location
成员,是的,但是
[Locations]
(又称
Array
)没有。天哪,你完全正确,谢谢,那太愚蠢了
let withoutElevation = weather // Weather
    .Locations // [Locations]
    .Location // doesn't exist, but Location was intended
    .filter {$0.elevation == nil} // invalid, but [Location] was intended