diff --git a/Source/Charts/Animation/Animator.swift b/Source/Charts/Animation/Animator.swift index 1acb45198d..cec0987572 100644 --- a/Source/Charts/Animation/Animator.swift +++ b/Source/Charts/Animation/Animator.swift @@ -68,39 +68,27 @@ open class Animator: NSObject @objc open func stop() { - if _displayLink != nil + guard let _displayLink = _displayLink else { + return + } + _displayLink.remove(from: RunLoop.main, forMode: RunLoopMode.commonModes) + self._displayLink = nil + + _enabledX = false + _enabledY = false + + // If we stopped an animation in the middle, we do not want to leave it like this + if phaseX != 1.0 || phaseY != 1.0 { - _displayLink?.remove(from: RunLoop.main, forMode: RunLoopMode.commonModes) - _displayLink = nil - - _enabledX = false - _enabledY = false - - // If we stopped an animation in the middle, we do not want to leave it like this - if phaseX != 1.0 || phaseY != 1.0 - { - phaseX = 1.0 - phaseY = 1.0 - - if delegate != nil - { - delegate!.animatorUpdated(self) - } - if updateBlock != nil - { - updateBlock!() - } - } - - if delegate != nil - { - delegate!.animatorStopped(self) - } - if stopBlock != nil - { - stopBlock?() - } + phaseX = 1.0 + phaseY = 1.0 + + delegate?.animatorUpdated(self) + updateBlock?() } + + delegate?.animatorStopped(self) + stopBlock?() } fileprivate func updateAnimationPhases(_ currentTime: TimeInterval) @@ -115,14 +103,7 @@ open class Animator: NSObject elapsed = duration } - if _easingX != nil - { - phaseX = _easingX!(elapsed, duration) - } - else - { - phaseX = Double(elapsed / duration) - } + phaseX = _easingX?(elapsed, duration) ?? Double(elapsed / duration) } if _enabledY @@ -135,9 +116,9 @@ open class Animator: NSObject elapsed = duration } - if _easingY != nil + if let _easingY = _easingY { - phaseY = _easingY!(elapsed, duration) + phaseY = _easingY(elapsed, duration) } else { @@ -151,15 +132,10 @@ open class Animator: NSObject let currentTime: TimeInterval = CACurrentMediaTime() updateAnimationPhases(currentTime) - - if delegate != nil - { - delegate!.animatorUpdated(self) - } - if updateBlock != nil - { - updateBlock!() - } + + delegate?.animatorUpdated(self) + + updateBlock?() if currentTime >= _endTime { diff --git a/Source/Charts/Charts/BarLineChartViewBase.swift b/Source/Charts/Charts/BarLineChartViewBase.swift index ff46f88d4e..135b707728 100644 --- a/Source/Charts/Charts/BarLineChartViewBase.swift +++ b/Source/Charts/Charts/BarLineChartViewBase.swift @@ -163,11 +163,8 @@ open class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChartD open override func draw(_ rect: CGRect) { super.draw(rect) - - if _data === nil - { - return - } + + guard data != nil, let renderer = renderer else { return } let optionalContext = NSUIGraphicsGetCurrentContext() guard let context = optionalContext else { return } @@ -223,17 +220,17 @@ open class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChartD // make sure the data cannot be drawn outside the content-rect context.saveGState() context.clip(to: _viewPortHandler.contentRect) - renderer?.drawData(context: context) + renderer.drawData(context: context) // if highlighting is enabled if (valuesToHighlight()) { - renderer?.drawHighlighted(context: context, indices: _indicesToHighlight) + renderer.drawHighlighted(context: context, indices: _indicesToHighlight) } context.restoreGState() - renderer!.drawExtras(context: context) + renderer.drawExtras(context: context) if _xAxis.isEnabled && !_xAxis.isDrawLimitLinesBehindDataEnabled { @@ -259,13 +256,13 @@ open class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChartD context.saveGState() context.clip(to: _viewPortHandler.contentRect) - renderer!.drawValues(context: context) + renderer.drawValues(context: context) context.restoreGState() } else { - renderer!.drawValues(context: context) + renderer.drawValues(context: context) } _legendRenderer.renderLegend(context: context) @@ -529,19 +526,19 @@ open class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChartD if recognizer.state == NSUIGestureRecognizerState.ended { - if !self.isHighLightPerTapEnabled { return } + if !isHighLightPerTapEnabled { return } let h = getHighlightByTouchPoint(recognizer.location(in: self)) - if h === nil || h!.isEqual(self.lastHighlighted) + if h?.isEqual(lastHighlighted) ?? true { - self.highlightValue(nil, callDelegate: true) - self.lastHighlighted = nil + highlightValue(nil, callDelegate: true) + lastHighlighted = nil } else { - self.highlightValue(h, callDelegate: true) - self.lastHighlighted = h + highlightValue(h, callDelegate: true) + lastHighlighted = h } } } @@ -751,9 +748,9 @@ open class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChartD let lastHighlighted = self.lastHighlighted - if ((h === nil && lastHighlighted !== nil) || + if (h === nil && lastHighlighted !== nil) || (h !== nil && lastHighlighted === nil) || - (h !== nil && lastHighlighted !== nil && !h!.isEqual(lastHighlighted))) + (lastHighlighted !== nil && !(h?.isEqual(lastHighlighted) ?? true)) { self.lastHighlighted = h self.highlightValue(h, callDelegate: true) @@ -931,7 +928,7 @@ open class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChartD )) { var scrollView = self.superview - while (scrollView !== nil && !scrollView!.isKind(of: NSUIScrollView.self)) + while !(scrollView?.isKind(of: NSUIScrollView.self) ?? true) { scrollView = scrollView?.superview } @@ -946,23 +943,13 @@ open class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChartD var foundScrollView = scrollView as? NSUIScrollView - if foundScrollView !== nil && !foundScrollView!.nsuiIsScrollEnabled + if !(foundScrollView?.nsuiIsScrollEnabled ?? true) { foundScrollView = nil } - var scrollViewPanGestureRecognizer: NSUIGestureRecognizer! - - if foundScrollView !== nil - { - for scrollRecognizer in foundScrollView!.nsuiGestureRecognizers! - { - if scrollRecognizer.isKind(of: NSUIPanGestureRecognizer.self) - { - scrollViewPanGestureRecognizer = scrollRecognizer as! NSUIPanGestureRecognizer - break - } - } + let scrollViewPanGestureRecognizer = foundScrollView?.nsuiGestureRecognizers?.first { + $0 is NSUIPanGestureRecognizer } if otherGestureRecognizer === scrollViewPanGestureRecognizer diff --git a/Source/Charts/Charts/ChartViewBase.swift b/Source/Charts/Charts/ChartViewBase.swift index f72dad9ee9..0c79db9d74 100755 --- a/Source/Charts/Charts/ChartViewBase.swift +++ b/Source/Charts/Charts/ChartViewBase.swift @@ -249,15 +249,15 @@ open class ChartViewBase: NSUIView, ChartDataProvider, AnimatorDelegate _data = newValue _offsetsCalculated = false - if _data == nil + guard let _data = _data else { return } // calculate how many digits are needed - setupDefaultFormatter(min: _data!.getYMin(), max: _data!.getYMax()) + setupDefaultFormatter(min: _data.getYMin(), max: _data.getYMax()) - for set in _data!.dataSets + for set in _data.dataSets { if set.needsFormatter || set.valueFormatter === _defaultValueFormatter { @@ -394,16 +394,8 @@ open class ChartViewBase: NSUIView, ChartDataProvider, AnimatorDelegate descriptionText.count > 0 else { return } - var position = description.position - - // if no position specified, draw on default position - if position == nil - { - let frame = self.bounds - position = CGPoint( - x: frame.width - _viewPortHandler.offsetRight - description.xOffset, - y: frame.height - _viewPortHandler.offsetBottom - description.yOffset - description.font.lineHeight) - } + let position = description.position ?? CGPoint(x: bounds.width - _viewPortHandler.offsetRight - description.xOffset, + y: bounds.height - _viewPortHandler.offsetBottom - description.yOffset - description.font.lineHeight) var attrs = [NSAttributedStringKey : Any]() @@ -413,7 +405,7 @@ open class ChartViewBase: NSUIView, ChartDataProvider, AnimatorDelegate ChartUtils.drawText( context: context, text: descriptionText, - point: position!, + point: position, align: description.textAlign, attributes: attrs) } @@ -558,16 +550,16 @@ open class ChartViewBase: NSUIView, ChartDataProvider, AnimatorDelegate } } - if callDelegate && delegate != nil + if callDelegate, let delegate = delegate { - if h == nil + if let h = h { - delegate!.chartValueNothingSelected?(self) + // notify the listener + delegate.chartValueSelected?(self, entry: entry!, highlight: h) } else { - // notify the listener - delegate!.chartValueSelected?(self, entry: entry!, highlight: h!) + delegate.chartValueNothingSelected?(self) } } @@ -871,24 +863,20 @@ open class ChartViewBase: NSUIView, ChartDataProvider, AnimatorDelegate /// - returns: `true` if the image was saved successfully open func save(to path: String, format: ImageFormat, compressionQuality: Double) -> Bool { - guard let image = getChartImage(transparent: format != .jpeg) - else { return false } + guard let image = getChartImage(transparent: format != .jpeg) else { return false } - var imageData: Data! + let imageData: Data? switch (format) { - case .png: - imageData = NSUIImagePNGRepresentation(image) - break - - case .jpeg: - imageData = NSUIImageJPEGRepresentation(image, CGFloat(compressionQuality)) - break + case .png: imageData = NSUIImagePNGRepresentation(image) + case .jpeg: imageData = NSUIImageJPEGRepresentation(image, CGFloat(compressionQuality)) } + guard let data = imageData else { return false } + do { - try imageData.write(to: URL(fileURLWithPath: path), options: .atomic) + try data.write(to: URL(fileURLWithPath: path), options: .atomic) } catch { diff --git a/Source/Charts/Charts/CombinedChartView.swift b/Source/Charts/Charts/CombinedChartView.swift index 5ae3006fc6..eef738f58b 100644 --- a/Source/Charts/Charts/CombinedChartView.swift +++ b/Source/Charts/Charts/CombinedChartView.swift @@ -55,7 +55,7 @@ open class CombinedChartView: BarLineChartViewBase, CombinedChartDataProvider self.highlighter = CombinedHighlighter(chart: self, barDataProvider: self) - (renderer as! CombinedChartRenderer?)!.createRenderers() + (renderer as? CombinedChartRenderer)?.createRenderers() renderer?.initBuffers() } } @@ -116,11 +116,7 @@ open class CombinedChartView: BarLineChartViewBase, CombinedChartDataProvider { get { - if _data === nil - { - return nil - } - return (_data as! CombinedChartData!).lineData + return combinedData?.lineData } } @@ -130,11 +126,7 @@ open class CombinedChartView: BarLineChartViewBase, CombinedChartDataProvider { get { - if _data === nil - { - return nil - } - return (_data as! CombinedChartData!).barData + return combinedData?.barData } } @@ -144,11 +136,7 @@ open class CombinedChartView: BarLineChartViewBase, CombinedChartDataProvider { get { - if _data === nil - { - return nil - } - return (_data as! CombinedChartData!).scatterData + return combinedData?.scatterData } } @@ -158,11 +146,7 @@ open class CombinedChartView: BarLineChartViewBase, CombinedChartDataProvider { get { - if _data === nil - { - return nil - } - return (_data as! CombinedChartData!).candleData + return combinedData?.candleData } } @@ -172,11 +156,7 @@ open class CombinedChartView: BarLineChartViewBase, CombinedChartDataProvider { get { - if _data === nil - { - return nil - } - return (_data as! CombinedChartData!).bubbleData + return combinedData?.bubbleData } } diff --git a/Source/Charts/Charts/PieChartView.swift b/Source/Charts/Charts/PieChartView.swift index c6c17aaa93..7316c2fe03 100755 --- a/Source/Charts/Charts/PieChartView.swift +++ b/Source/Charts/Charts/PieChartView.swift @@ -102,18 +102,21 @@ open class PieChartView: PieRadarChartViewBase } let optionalContext = NSUIGraphicsGetCurrentContext() - guard let context = optionalContext else { return } + guard let context = optionalContext, let renderer = renderer else + { + return + } - renderer!.drawData(context: context) + renderer.drawData(context: context) if (valuesToHighlight()) { - renderer!.drawHighlighted(context: context, indices: _indicesToHighlight) + renderer.drawHighlighted(context: context, indices: _indicesToHighlight) } - renderer!.drawExtras(context: context) + renderer.drawExtras(context: context) - renderer!.drawValues(context: context) + renderer.drawValues(context: context) _legendRenderer.renderLegend(context: context) diff --git a/Source/Charts/Charts/PieRadarChartViewBase.swift b/Source/Charts/Charts/PieRadarChartViewBase.swift index 87944f55f3..b45e2157d3 100755 --- a/Source/Charts/Charts/PieRadarChartViewBase.swift +++ b/Source/Charts/Charts/PieRadarChartViewBase.swift @@ -553,13 +553,9 @@ open class PieRadarChartViewBase: ChartViewBase { stopDeceleration() - if !rotationWithTwoFingers + if !rotationWithTwoFingers, let touchLocation = touches.first?.location(in: self) { - let touch = touches.first as NSUITouch! - - let touchLocation = touch?.location(in: self) - - processRotationGestureBegan(location: touchLocation!) + processRotationGestureBegan(location: touchLocation) } } @@ -571,13 +567,10 @@ open class PieRadarChartViewBase: ChartViewBase open override func nsuiTouchesMoved(_ touches: Set, withEvent event: NSUIEvent?) { - if rotationEnabled && !rotationWithTwoFingers + if rotationEnabled && !rotationWithTwoFingers, let touch = touches.first { - let touch = touches.first as NSUITouch! - - let touchLocation = touch?.location(in: self) - - processRotationGestureMoved(location: touchLocation!) + let touchLocation = touch.location(in: self) + processRotationGestureMoved(location: touchLocation) } if !_isRotating @@ -593,13 +586,10 @@ open class PieRadarChartViewBase: ChartViewBase super.nsuiTouchesEnded(touches, withEvent: event) } - if rotationEnabled && !rotationWithTwoFingers + if rotationEnabled && !rotationWithTwoFingers, let touch = touches.first { - let touch = touches.first as NSUITouch! - - let touchLocation = touch?.location(in: self) - - processRotationGestureEnded(location: touchLocation!) + let touchLocation = touch.location(in: self) + processRotationGestureEnded(location: touchLocation) } if _isRotating diff --git a/Source/Charts/Charts/RadarChartView.swift b/Source/Charts/Charts/RadarChartView.swift index ed74f75194..232d59492f 100644 --- a/Source/Charts/Charts/RadarChartView.swift +++ b/Source/Charts/Charts/RadarChartView.swift @@ -101,10 +101,7 @@ open class RadarChartView: PieRadarChartViewBase { super.draw(rect) - if _data === nil - { - return - } + guard data != nil, let renderer = renderer else { return } let optionalContext = NSUIGraphicsGetCurrentContext() guard let context = optionalContext else { return } @@ -118,7 +115,7 @@ open class RadarChartView: PieRadarChartViewBase if drawWeb { - renderer!.drawExtras(context: context) + renderer.drawExtras(context: context) } if _yAxis.isEnabled && _yAxis.isDrawLimitLinesBehindDataEnabled @@ -126,11 +123,11 @@ open class RadarChartView: PieRadarChartViewBase _yAxisRenderer.renderLimitLines(context: context) } - renderer!.drawData(context: context) + renderer.drawData(context: context) if valuesToHighlight() { - renderer!.drawHighlighted(context: context, indices: _indicesToHighlight) + renderer.drawHighlighted(context: context, indices: _indicesToHighlight) } if _yAxis.isEnabled && !_yAxis.isDrawLimitLinesBehindDataEnabled @@ -140,7 +137,7 @@ open class RadarChartView: PieRadarChartViewBase _yAxisRenderer.renderAxisLabels(context: context) - renderer!.drawValues(context: context) + renderer.drawValues(context: context) _legendRenderer.renderLegend(context: context) diff --git a/Source/Charts/Components/MarkerImage.swift b/Source/Charts/Components/MarkerImage.swift index 0754909378..14e4dcfc86 100644 --- a/Source/Charts/Components/MarkerImage.swift +++ b/Source/Charts/Components/MarkerImage.swift @@ -82,17 +82,19 @@ open class MarkerImage: NSObject, IMarker open func draw(context: CGContext, point: CGPoint) { - let offset = self.offsetForDrawing(atPoint: point) + guard let image = image else { return } + + let offset = offsetForDrawing(atPoint: point) var size = self.size - if size.width == 0.0 && image != nil + if size.width == 0.0 { - size.width = image?.size.width ?? 0.0 + size.width = image.size.width } - if size.height == 0.0 && image != nil + if size.height == 0.0 { - size.height = image?.size.height ?? 0.0 + size.height = image.size.height } let rect = CGRect( @@ -102,7 +104,7 @@ open class MarkerImage: NSObject, IMarker height: size.height) NSUIGraphicsPushContext(context) - image!.draw(in: rect) + image.draw(in: rect) NSUIGraphicsPopContext() } } diff --git a/Source/Charts/Components/MarkerView.swift b/Source/Charts/Components/MarkerView.swift index 886fd275d7..a051ff9968 100644 --- a/Source/Charts/Components/MarkerView.swift +++ b/Source/Charts/Components/MarkerView.swift @@ -25,9 +25,9 @@ open class MarkerView: NSUIView, IMarker open func offsetForDrawing(atPoint point: CGPoint) -> CGPoint { - var offset = self.offset + guard let chart = chartView else { return self.offset } - let chart = self.chartView + var offset = self.offset let width = self.bounds.size.width let height = self.bounds.size.height @@ -36,18 +36,18 @@ open class MarkerView: NSUIView, IMarker { offset.x = -point.x } - else if chart != nil && point.x + width + offset.x > chart!.bounds.size.width + else if point.x + width + offset.x > chart.bounds.size.width { - offset.x = chart!.bounds.size.width - point.x - width + offset.x = chart.bounds.size.width - point.x - width } if point.y + offset.y < 0 { offset.y = -point.y } - else if chart != nil && point.y + height + offset.y > chart!.bounds.size.height + else if point.y + height + offset.y > chart.bounds.size.height { - offset.y = chart!.bounds.size.height - point.y - height + offset.y = chart.bounds.size.height - point.y - height } return offset diff --git a/Source/Charts/Data/Implementations/Standard/BarChartData.swift b/Source/Charts/Data/Implementations/Standard/BarChartData.swift index 42a49cbf1a..0317fc8a6b 100644 --- a/Source/Charts/Data/Implementations/Standard/BarChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/BarChartData.swift @@ -61,8 +61,7 @@ open class BarChartData: BarLineScatterCandleBubbleChartData let start = fromX fromX += groupSpaceWidthHalf - for set in _dataSets as! [IBarChartDataSet] - { + (_dataSets as? [IBarChartDataSet])?.forEach { set in fromX += barSpaceHalf fromX += barWidthHalf diff --git a/Source/Charts/Data/Implementations/Standard/BarChartDataEntry.swift b/Source/Charts/Data/Implementations/Standard/BarChartDataEntry.swift index fda773e88e..89243992ad 100644 --- a/Source/Charts/Data/Implementations/Standard/BarChartDataEntry.swift +++ b/Source/Charts/Data/Implementations/Standard/BarChartDataEntry.swift @@ -102,17 +102,17 @@ open class BarChartDataEntry: ChartDataEntry @objc open func sumBelow(stackIndex :Int) -> Double { - if _yVals == nil + guard let yVals = _yVals else { return 0 } var remainder: Double = 0.0 - var index = _yVals!.count - 1 + var index = yVals.count - 1 while (index > stackIndex && index >= 0) { - remainder += _yVals![index] + remainder += yVals[index] index -= 1 } @@ -133,7 +133,7 @@ open class BarChartDataEntry: ChartDataEntry @objc open func calcPosNegSum() { - if _yVals == nil + guard let _yVals = _yVals else { _positiveSum = 0.0 _negativeSum = 0.0 @@ -143,7 +143,7 @@ open class BarChartDataEntry: ChartDataEntry var sumNeg: Double = 0.0 var sumPos: Double = 0.0 - for f in _yVals! + for f in _yVals { if f < 0.0 { diff --git a/Source/Charts/Data/Implementations/Standard/BubbleChartData.swift b/Source/Charts/Data/Implementations/Standard/BubbleChartData.swift index b07e1be421..433f384f75 100644 --- a/Source/Charts/Data/Implementations/Standard/BubbleChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/BubbleChartData.swift @@ -27,9 +27,6 @@ open class BubbleChartData: BarLineScatterCandleBubbleChartData /// Sets the width of the circle that surrounds the bubble when highlighted for all DataSet objects this data object contains @objc open func setHighlightCircleWidth(_ width: CGFloat) { - for set in (_dataSets as? [IBubbleChartDataSet])! - { - set.highlightCircleWidth = width - } + (_dataSets as? [IBubbleChartDataSet])?.forEach { $0.highlightCircleWidth = width } } } diff --git a/Source/Charts/Data/Implementations/Standard/LineChartDataSet.swift b/Source/Charts/Data/Implementations/Standard/LineChartDataSet.swift index 391bba48a1..eae20419cb 100644 --- a/Source/Charts/Data/Implementations/Standard/LineChartDataSet.swift +++ b/Source/Charts/Data/Implementations/Standard/LineChartDataSet.swift @@ -187,14 +187,7 @@ open class LineChartDataSet: LineRadarChartDataSet, ILineChartDataSet } set { - if newValue == nil - { - _fillFormatter = DefaultFillFormatter() - } - else - { - _fillFormatter = newValue! - } + _fillFormatter = newValue ?? DefaultFillFormatter() } } diff --git a/Source/Charts/Data/Implementations/Standard/PieChartData.swift b/Source/Charts/Data/Implementations/Standard/PieChartData.swift index 02ae0d586f..f2dc35a948 100644 --- a/Source/Charts/Data/Implementations/Standard/PieChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/PieChartData.swift @@ -31,9 +31,9 @@ open class PieChartData: ChartData } set { - if newValue != nil + if let newValue = newValue { - dataSets = [newValue!] + dataSets = [newValue] } else { @@ -60,7 +60,7 @@ open class PieChartData: ChartData if ignorecase { - if (label.caseInsensitiveCompare(dataSets[0].label!) == ComparisonResult.orderedSame) + if let label = dataSets[0].label, label.caseInsensitiveCompare(label) == .orderedSame { return dataSets[0] } diff --git a/Source/Charts/Data/Implementations/Standard/ScatterChartData.swift b/Source/Charts/Data/Implementations/Standard/ScatterChartData.swift index 9124a733c0..ff8ccaf93e 100644 --- a/Source/Charts/Data/Implementations/Standard/ScatterChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/ScatterChartData.swift @@ -37,14 +37,9 @@ open class ScatterChartData: BarLineScatterCandleBubbleChartData { print("ScatterChartData: Found a DataSet which is not a ScatterChartDataSet", terminator: "\n") } - else + else if let size = scatterDataSet?.scatterShapeSize, size > max { - let size = scatterDataSet!.scatterShapeSize - - if size > max - { - max = size - } + max = size } }