继承,协议和静态字段

时间:2015-03-12 07:41:05

标签: ios swift macos

如何在idcommonStaticField字段中删除重复内容?

struct S1 {
  let id: String       //note - it's let
  static let commonStaticField = "s1" //note - it's static
  let someField1: String
  let someField2: String
}

struct S2 {
  let id: String
  static let commonStaticField = "s2"
  let someField3: String
  let someField4: String
}

我尝试使用不同的变体:

protocol P1 {
  let id: String
  static let commonStaticField: String //static not allowed
}

但它并没有成功。我希望能够做到这一点:

struct S1: P1 {
  static let commonStaticField = "s1" //note - it's static and defined
  let someField1: String
  let someField2: String
}

let s1 = S1(id: "some id", someField1: "some field3", someField2: "some field2")

0 个答案:

没有答案