From 908427e8b0c94f0c632da09fe75b0026c61b4c2f Mon Sep 17 00:00:00 2001 From: bre7 Date: Fri, 18 Mar 2016 23:53:49 -0300 Subject: [PATCH 1/2] Generics & minor styling fixes --- FSLineChart/FSLineChart/FSLineChart.h | 2 +- FSLineChart/FSLineChart/FSLineChart.m | 29 +++++++++++---------------- 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/FSLineChart/FSLineChart/FSLineChart.h b/FSLineChart/FSLineChart/FSLineChart.h index 0b0989e..29430a8 100644 --- a/FSLineChart/FSLineChart/FSLineChart.h +++ b/FSLineChart/FSLineChart/FSLineChart.h @@ -87,7 +87,7 @@ typedef NS_ENUM(NSInteger, ValueLabelPositionType) { @property (nonatomic) CGFloat animationDuration; // Set the actual data for the chart, and then render it to the view. -- (void)setChartData:(NSArray *)chartData; +- (void)setChartData:(nonnull NSArray *)chartData; // Clear all rendered data from the view. - (void)clearChartData; diff --git a/FSLineChart/FSLineChart/FSLineChart.m b/FSLineChart/FSLineChart/FSLineChart.m index b5390d4..2a75fc4 100644 --- a/FSLineChart/FSLineChart/FSLineChart.m +++ b/FSLineChart/FSLineChart/FSLineChart.m @@ -25,8 +25,8 @@ @interface FSLineChart () -@property (nonatomic, strong) NSMutableArray* data; -@property (nonatomic, strong) NSMutableArray* layers; +@property (nonatomic, strong) NSMutableArray * data; +@property (nonatomic, strong) NSMutableArray * layers; @property (nonatomic) CGFloat min; @property (nonatomic) CGFloat max; @@ -64,7 +64,7 @@ - (void)awakeFromNib - (void)commonInit { - _layers = [NSMutableArray array]; + _layers = [NSMutableArray array]; self.backgroundColor = [UIColor whiteColor]; [self setDefaultParameters]; } @@ -113,9 +113,8 @@ - (void)layoutSubviews [obj removeFromSuperview]; }]; - [self.layers enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { - CALayer* layer = (CALayer*)obj; - [layer removeFromSuperlayer]; + [self.layers enumerateObjectsUsingBlock:^(__kindof CALayer * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { + [obj removeFromSuperlayer]; }]; [self layoutChart]; @@ -164,12 +163,8 @@ - (void)layoutChart [self setNeedsDisplay]; } -- (void)setChartData:(NSArray *)chartData +- (void)setChartData:(nonnull NSArray *)chartData { - if (chartData == nil || chartData.count == 0) { - return; - } - _data = [NSMutableArray arrayWithArray:chartData]; [self layoutChart]; } @@ -271,7 +266,7 @@ - (void)drawGrid CGContextAddLineToPoint(ctx, _margin, _axisHeight + _margin + 3); CGContextStrokePath(ctx); - CGFloat scale = [self horizontalScale]; + CGFloat scale = [self horizontalScale]; CGFloat minBound = [self minVerticalBound]; CGFloat maxBound = [self maxVerticalBound]; @@ -336,10 +331,10 @@ - (void)strokeChart } UIBezierPath *noPath = [self getLinePath:0 withSmoothing:_bezierSmoothing close:NO]; - UIBezierPath *path = [self getLinePath:scale withSmoothing:_bezierSmoothing close:NO]; - + UIBezierPath *path = [self getLinePath:scale withSmoothing:_bezierSmoothing close:NO]; + UIBezierPath *noFill = [self getLinePath:0 withSmoothing:_bezierSmoothing close:YES]; - UIBezierPath *fill = [self getLinePath:scale withSmoothing:_bezierSmoothing close:YES]; + UIBezierPath *fill = [self getLinePath:scale withSmoothing:_bezierSmoothing close:YES]; if(_fillColor) { CAShapeLayer* fillLayer = [CAShapeLayer layer]; @@ -537,7 +532,7 @@ - (CGFloat)getUpperRoundNumber:(CGFloat)value forGridStep:(int)gridStep - (void)setGridStep:(int)gridStep { - _verticalGridStep = gridStep; + _verticalGridStep = gridStep; _horizontalGridStep = gridStep; } @@ -589,7 +584,7 @@ - (UIBezierPath*)getLinePath:(float)scale withSmoothing:(BOOL)smoothed close:(BO controlPoint[0].y = p.y + m.y * _bezierSmoothingTension; // Second control point - nextPoint = [self getPointForIndex:i + 2 withScale:scale]; + nextPoint = [self getPointForIndex:i + 2 withScale:scale]; previousPoint = [self getPointForIndex:i withScale:scale]; p = [self getPointForIndex:i + 1 withScale:scale]; m = CGPointZero; From b12dc0424404d56ea54ca1b52d407fb012d2526a Mon Sep 17 00:00:00 2001 From: bre7 Date: Sat, 19 Mar 2016 00:18:53 -0300 Subject: [PATCH 2/2] Nullability annotations --- FSLineChart/FSLineChart/FSLineChart.h | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/FSLineChart/FSLineChart/FSLineChart.h b/FSLineChart/FSLineChart/FSLineChart.h index 29430a8..d6df802 100644 --- a/FSLineChart/FSLineChart/FSLineChart.h +++ b/FSLineChart/FSLineChart/FSLineChart.h @@ -21,13 +21,15 @@ #import +NS_ASSUME_NONNULL_BEGIN + @interface FSLineChart : UIView // Block definition for getting a label for a set index (use case: date, units,...) -typedef NSString *(^FSLabelForIndexGetter)(NSUInteger index); +typedef NSString * _Nullable (^FSLabelForIndexGetter)(NSUInteger index); // Same as above, but for the value (for adding a currency, or a unit symbol for example) -typedef NSString *(^FSLabelForValueGetter)(CGFloat value); +typedef NSString * _Nullable (^FSLabelForValueGetter)(CGFloat value); typedef NS_ENUM(NSInteger, ValueLabelPositionType) { ValueLabelLeft, @@ -36,13 +38,13 @@ typedef NS_ENUM(NSInteger, ValueLabelPositionType) { }; // Index label properties -@property (copy) FSLabelForIndexGetter labelForIndex; +@property (copy, nullable) FSLabelForIndexGetter labelForIndex; @property (nonatomic, strong) UIFont* indexLabelFont; @property (nonatomic) UIColor* indexLabelTextColor; @property (nonatomic) UIColor* indexLabelBackgroundColor; // Value label properties -@property (copy) FSLabelForValueGetter labelForValue; +@property (copy, nullable) FSLabelForValueGetter labelForValue; @property (nonatomic, strong) UIFont* valueLabelFont; @property (nonatomic) UIColor* valueLabelTextColor; @property (nonatomic) UIColor* valueLabelBackgroundColor; @@ -65,7 +67,7 @@ typedef NS_ENUM(NSInteger, ValueLabelPositionType) { // Chart parameters @property (nonatomic, strong) UIColor* color; -@property (nonatomic, strong) UIColor* fillColor; +@property (nonatomic, strong, nullable) UIColor* fillColor; @property (nonatomic) CGFloat lineWidth; // Data points @@ -87,7 +89,7 @@ typedef NS_ENUM(NSInteger, ValueLabelPositionType) { @property (nonatomic) CGFloat animationDuration; // Set the actual data for the chart, and then render it to the view. -- (void)setChartData:(nonnull NSArray *)chartData; +- (void)setChartData:(NSArray *)chartData; // Clear all rendered data from the view. - (void)clearChartData; @@ -96,5 +98,6 @@ typedef NS_ENUM(NSInteger, ValueLabelPositionType) { - (CGFloat)minVerticalBound; - (CGFloat)maxVerticalBound; - @end + +NS_ASSUME_NONNULL_END