访问嵌套结构

时间:2017-11-07 06:27:35

标签: swift

我有一个结构Student,它包含另一个结构Address

public struct Student {
  public struct Address {
     public static let street = "xyz"
  }
}

在学生的单元测试中,我尝试通过以下方式访问街道:

 switch(someStr) {
     case is Student.Address.street:
      ...
    }

我收到编译错误:Static let 'street' is not a member type of 'Student.Address'为什么?如何通过street访问Student呢?

此问题仅在单元测试中。

2 个答案:

答案 0 :(得分:2)

您尚未在街道变量中定义其类型varlet

检查以下内容:

public struct Student {
  public struct Address {
     public static let street = "xyz"
  }
}

let street = Student.Address.street

switch(street) {

case Student.Address.street:
    print("vishal")
    break
default:
    break
}

答案 1 :(得分:0)

您错过了let

public struct Student {
    public struct Address {
        public static let street = "xyz"
    }
}