Swift:更新结构变量?

时间:2016-11-27 21:44:36

标签: swift struct

我对Swift相当新,我今天偶然发现了这个问题,如果有答案的话,这对我有帮助。下面的代码解释了它:

struct Foo {
  var x: Float
  mutating func change() {
    x += 1
  }
}

struct Bar {
  var y: Float
}

var foo = Foo(x: 1)
let bar = Bar(y: foo.x)

foo.change()

print(foo.x) // 2.0
print(bar.y) // 1.0 (how can I make this 2.0 as well?)

我以为我可能不得不使用指针,但我不知道怎么做,而且我不知道如果它甚至可以工作。 任何想法都会有所帮助!

2 个答案:

答案 0 :(得分:1)

在此处获取所需内容的一种方法是定义{ "id":"#", "definitions":{ "EmployeeWithInnerClass":{ "type":"object", "title":"EmployeeWithInnerClass", "required":[ "firstName", "lastName", "address" ], "properties":{ "firstName":{ "title":"firstName", "allOf":[ { "$ref":"http://www.jsonix.org/jsonschemas/w3c/2001/XMLSchema.jsonschema#/definitions/string" } ], "propertyType":"element", "elementName":{ "localPart":"FirstName", "namespaceURI":"" } }, "birthDate":{ "title":"birthDate", "allOf":[ { "$ref":"http://www.jsonix.org/jsonschemas/w3c/2001/XMLSchema.jsonschema#/definitions/date" } ], "propertyType":"element", "elementName":{ "localPart":"BirthDate", "namespaceURI":"" } }, "lastName":{ "title":"lastName", "allOf":[ { "$ref":"http://www.jsonix.org/jsonschemas/w3c/2001/XMLSchema.jsonschema#/definitions/string" } ], "propertyType":"element", "elementName":{ "localPart":"LastName", "namespaceURI":"" } }, "address":{ "title":"address", "allOf":[ { "$ref":"#/definitions/EmployeeWithInnerClass.Address" } ], "propertyType":"element", "elementName":{ "localPart":"Address", "namespaceURI":"" } } }, "typeType":"classInfo", "propertiesOrder":[ "firstName", "birthDate", "lastName", "address" ] }, "EmployeeWithInnerClass.Address":{ "type":"object", "title":"EmployeeWithInnerClass.Address", "required":[ "street" ], "properties":{ "street":{ "title":"street", "allOf":[ { "$ref":"http://www.jsonix.org/jsonschemas/w3c/2001/XMLSchema.jsonschema#/definitions/string" } ], "propertyType":"element", "elementName":{ "localPart":"Street", "namespaceURI":"" } } }, "typeType":"classInfo", "propertiesOrder":[ "street" ] }, "EmployeeType":{ "type":"object", "title":"EmployeeType", "required":[ "firstName", "lastName" ], "properties":{ "firstName":{ "title":"firstName", "allOf":[ { "$ref":"http://www.jsonix.org/jsonschemas/w3c/2001/XMLSchema.jsonschema#/definitions/string" } ], "propertyType":"element", "elementName":{ "localPart":"FirstName", "namespaceURI":"" } }, "birthDate":{ "title":"birthDate", "allOf":[ { "$ref":"http://www.jsonix.org/jsonschemas/w3c/2001/XMLSchema.jsonschema#/definitions/date" } ], "propertyType":"element", "elementName":{ "localPart":"BirthDate", "namespaceURI":"" } }, "lastName":{ "title":"lastName", "allOf":[ { "$ref":"http://www.jsonix.org/jsonschemas/w3c/2001/XMLSchema.jsonschema#/definitions/string" } ], "propertyType":"element", "elementName":{ "localPart":"LastName", "namespaceURI":"" } } }, "typeType":"classInfo", "typeName":{ "localPart":"EmployeeType", "namespaceURI":"http://jaxb.vo.dozer.org/Employee" }, "propertiesOrder":[ "firstName", "birthDate", "lastName" ] } }, "anyOf":[ { "type":"object", "properties":{ "name":{ "allOf":[ { "$ref":"http://www.jsonix.org/jsonschemas/w3c/2001/XMLSchema.jsonschema#/definitions/QName" }, { "type":"object", "properties":{ "localPart":{ "enum":[ "Employee" ] }, "namespaceURI":{ "enum":[ "http://jaxb.vo.dozer.org/Employee" ] } } } ] }, "value":{ "$ref":"#/definitions/EmployeeType" } }, "elementName":{ "localPart":"Employee", "namespaceURI":"http://jaxb.vo.dozer.org/Employee" } }, { "type":"object", "properties":{ "name":{ "allOf":[ { "$ref":"http://www.jsonix.org/jsonschemas/w3c/2001/XMLSchema.jsonschema#/definitions/QName" }, { "type":"object", "properties":{ "localPart":{ "enum":[ "EmployeeWithInnerClass" ] }, "namespaceURI":{ "enum":[ "http://jaxb.vo.dozer.org/Employee" ] } } } ] }, "value":{ "$ref":"#/definitions/EmployeeWithInnerClass" } }, "elementName":{ "localPart":"EmployeeWithInnerClass", "namespaceURI":"http://jaxb.vo.dozer.org/Employee" } } ] } 以保留对Bar的引用。并且由于您希望更改Foo的实例以继承Foo所持有的引用,因此您需要使Bar成为类而不是结构。

这是一个可能的解决方案:

Foo

答案 1 :(得分:0)

写作时

let bar = Bar(y: foo.x)

foo.x1)在Bar内被复制

所以foo.xbar.x之间没有进一步的联系。

这是因为在Swift中,params是按值传递的。

解决方案

首先,我们需要一个通用的包装器

class Wrapper<Element> {
    var element: Element
    init(element: Element) {
        self.element = element
    }
}

现在我们可以重新定义FooBar

struct Foo {
    var x: Wrapper<Float>
    mutating func change() {
        x.element += 1
    }
}

struct Bar {
    var y: Wrapper<Float>
}
  

现在您可以看到FooBar不会保留Float值,而是对Float值的引用。我们将使用它来使foo和bar值引用相同的Float值。

最后,我们可以测试两个结构中的值是否发生了变化

var foo = Foo(x: Wrapper<Float>(element: 1))
let bar = Bar(y: foo.x)
foo.change()

print(foo.x.element) // prints 2.0
print(bar.y.element) // prints 2.0