Optional Kind
cyber-gh opened this issue · 2 comments
cyber-gh commented
So i've got a class where most of the properties are optional and I'm trying to set their values.
Is it possible to set the value of an optional kind property? When i currently try it throws an RuntimeError.couldNotGetPointer. Is there a workaround to cast it as non-optional or something like that?
Thanks in advance
wickwirew commented
Thanks for the issue! I'm having trouble reproducing this. Could you provide a test case?
I've tried this and it passes.
func testOptionalProperty() throws {
class TestOptional {
var a: Int?
}
let info = try typeInfo(of: TestOptional.self)
let property = try info.property(named: "a")
var obj = TestOptional()
try property.set(value: 5, on: &obj)
XCTAssertEqual(5, obj.a)
}
cyber-gh commented
I'm sorry, it was my bad, didn't understand the thrown error. Apparently i was calling property.set on an optional object. Force unwrapping solved the problem.
try property.set(value: 5, on: &obj!)