From 7a5f59c7557360d286a25449a57381df07ee94fe Mon Sep 17 00:00:00 2001 From: shipinev Date: Fri, 2 Dec 2016 15:10:52 +0700 Subject: [PATCH] Fix #68. Change SVGParser.parse signature --- Example/Example/Examples/SVGChartsView.swift | 2 +- Source/svg/SVGParser.swift | 16 +++++++++++----- Source/svg/SVGView.swift | 6 +++--- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/Example/Example/Examples/SVGChartsView.swift b/Example/Example/Examples/SVGChartsView.swift index f42c338f..a5dff316 100644 --- a/Example/Example/Examples/SVGChartsView.swift +++ b/Example/Example/Examples/SVGChartsView.swift @@ -4,7 +4,7 @@ import Macaw class SVGChartsView: MacawView { required init?(coder aDecoder: NSCoder) { - super.init(node: SVGParser.parse(path: "pie-chart"), coder: aDecoder) + super.init(node: SVGParser.parse(path: "pie-chart") ?? Group(), coder: aDecoder) } } diff --git a/Source/svg/SVGParser.swift b/Source/svg/SVGParser.swift index 8c846ec8..0cefac9f 100644 --- a/Source/svg/SVGParser.swift +++ b/Source/svg/SVGParser.swift @@ -9,15 +9,21 @@ open class SVGParser { /// Parse an SVG file identified by the specified bundle, name and file extension. /// - returns: Root node of the corresponding Macaw scene. - open class func parse(bundle: Bundle, path: String, ofType: String = "svg") -> Node { - let path = bundle.path(forResource: path, ofType: ofType) - let text = try! String(contentsOfFile: path!, encoding: String.Encoding.utf8) - return SVGParser.parse(text: text) + open class func parse(bundle: Bundle, path: String, ofType: String = "svg") -> Node? { + guard let path = bundle.path(forResource: path, ofType: ofType) else { + return .none + } + do { + let text = try String(contentsOfFile: path, encoding: String.Encoding.utf8) + return SVGParser.parse(text: text) + } catch _ { + return .none + } } /// Parse an SVG file identified by the specified name and file extension. /// - returns: Root node of the corresponding Macaw scene. - open class func parse(path: String, ofType: String = "svg") -> Node { + open class func parse(path: String, ofType: String = "svg") -> Node? { return SVGParser.parse(bundle: Bundle.main, path: path, ofType: ofType) } diff --git a/Source/svg/SVGView.swift b/Source/svg/SVGView.swift index 34dc14c0..32425ec8 100644 --- a/Source/svg/SVGView.swift +++ b/Source/svg/SVGView.swift @@ -16,10 +16,10 @@ open class SVGView: MacawView { private let rootNode = Group() private func render() { + guard let svgNode = SVGParser.parse(path: fileName ?? "") else { + return + } let viewBounds = self.bounds - let svgNode = SVGParser.parse( - path: fileName ?? "" - ) if let nodeBounds = svgNode.bounds()?.cgRect() { let svgWidth = nodeBounds.origin.x + nodeBounds.width let svgHeight = nodeBounds.origin.y + nodeBounds.height