From 0074ee1d4a7f62ff7244c8d8d5d0eec65fe563ca Mon Sep 17 00:00:00 2001 From: Ilya Puchka Date: Tue, 11 Sep 2018 18:12:27 +0100 Subject: [PATCH 1/2] check for property via selector before using value(forKey:) --- CHANGELOG.md | 4 +++- Sources/Variable.swift | 4 +++- Tests/StencilTests/VariableSpec.swift | 6 ++++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dc7377c..3ccc740 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,9 @@ ### Bug Fixes - _None_ +- Now accessing undefined keys in NSObject does not cause runtime crash and instead renders empty string. + [Ilya Puchka](https://github.com/ilyapuchka) + [#234](https://github.com/stencilproject/Stencil/pull/234) ### Breaking Changes diff --git a/Sources/Variable.swift b/Sources/Variable.swift index 7c24e8a..05b8738 100644 --- a/Sources/Variable.swift +++ b/Sources/Variable.swift @@ -104,7 +104,9 @@ public struct Variable : Equatable, Resolvable { #if os(Linux) return nil #else - current = object.value(forKey: bit) + if object.responds(to: Selector(bit)) { + current = object.value(forKey: bit) + } #endif } else if let value = current { current = Mirror(reflecting: value).getValue(for: bit) diff --git a/Tests/StencilTests/VariableSpec.swift b/Tests/StencilTests/VariableSpec.swift index 66947e0..8303481 100644 --- a/Tests/StencilTests/VariableSpec.swift +++ b/Tests/StencilTests/VariableSpec.swift @@ -148,6 +148,12 @@ func testVariable() { let result = try variable.resolve(context) as? String try expect(result) == "Foo" } + + $0.it("does not crash on KVO") { + let variable = Variable("object.fullname") + let result = try variable.resolve(context) as? String + try expect(result).to.beNil() + } #endif $0.it("can resolve a value via reflection") { From 8210fa57f14024bc4514dcd9d527b8dd3a847383 Mon Sep 17 00:00:00 2001 From: Ilya Puchka Date: Tue, 11 Sep 2018 18:14:51 +0100 Subject: [PATCH 2/2] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3ccc740..275e440 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ ### Bug Fixes -- Now accessing undefined keys in NSObject does not cause runtime crash and instead renders empty string. +- Now accessing undefined keys in NSObject does not cause runtime crash and instead renders empty string. [Ilya Puchka](https://github.com/ilyapuchka) [#234](https://github.com/stencilproject/Stencil/pull/234)