Skip to content

Commit b45bdec

Browse files
hepaKKeshepaKKes
authored andcommitted
removes warnings, adds three new tests on annotationArray;
initWithAnnotation now raises exception when any element of the annotation collection is not responding to mkannotation protocol; refactors a little bit the HKAnnotationArray class private implementation
1 parent c4d9e5a commit b45bdec

File tree

6 files changed

+76
-29
lines changed

6 files changed

+76
-29
lines changed

EPMapExtensions/HKAnnotationArray.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
/**
2525
@param annotations Annotation array which contains objects conforming to `MKAnnotation` protocol
26+
@exception raises exception when any element of the annotation collection is not responding to `MKAnnotation` protocol
2627
*/
2728

2829
- (id)initWithAnnotationArray:(NSArray *)annotations;

EPMapExtensions/HKAnnotationArray.m

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88

99
#import "HKAnnotationArray.h"
1010

11+
@interface HKAnnotationArray ()
12+
13+
void checkAnnotationsCompliance(NSArray *annotations, Protocol *p);
14+
15+
@end
16+
1117
@implementation HKAnnotationArray
1218

1319
#pragma mark - Obj Alloc/Dealloc
@@ -23,6 +29,7 @@ - (id)init
2329
- (id)initWithAnnotationArray:(NSArray *)annotations
2430
{
2531
if (self = [super init]) {
32+
checkAnnotationsCompliance(annotations, @protocol(MKAnnotation));
2633
_annotationArray = [[NSMutableArray alloc] initWithArray:annotations];
2734
}
2835
return self;
@@ -33,19 +40,17 @@ - (id)initWithAnnotationArray:(NSArray *)annotations
3340

3441
- (void)addAnnotations:(NSArray *)annotations
3542
{
36-
for (id annotation in annotations)
37-
if (! [annotation conformsToProtocol:@protocol(MKAnnotation)])
38-
[NSException raise:NSInternalInconsistencyException format:@"annotations should contains only objs conforms to MKAnnotation protocol"];
39-
43+
checkAnnotationsCompliance(annotations, @protocol(MKAnnotation));
4044
[_annotationArray addObjectsFromArray:annotations];
4145
}
4246

4347
- (void)addAnnotation:(id <MKAnnotation>)annotation
4448
{
45-
if ( ! [annotation conformsToProtocol:@protocol(MKAnnotation)] )
49+
if ( ! [annotation conformsToProtocol:@protocol(MKAnnotation)] ) {
4650
[NSException raise:NSInternalInconsistencyException format:@"annotation should respond to MKAnnotation protocol"];
47-
else
51+
} else {
4852
[_annotationArray addObject:annotation];
53+
}
4954
}
5055

5156
- (NSArray *)allAnnotations
@@ -91,16 +96,15 @@ - (void)removeAnnotationsOfKindOfClasses:(NSArray *)classes
9196

9297
- (void)removeAnnotation:(id<MKAnnotation>)annotation
9398
{
94-
if ( ! [annotation conformsToProtocol:@protocol(MKAnnotation)] )
99+
if ( ! [annotation conformsToProtocol:@protocol(MKAnnotation)] ) {
95100
[NSException raise:NSInternalInconsistencyException format:@"annotation should respond to MKAnnotation protocol"];
101+
}
96102
[_annotationArray removeObject:annotation];
97103
}
98104

99105
- (void)removeAnnotations:(NSArray *)annotations
100106
{
101-
for (id annotation in annotations)
102-
if (! [annotation conformsToProtocol:@protocol(MKAnnotation)])
103-
[NSException raise:NSInternalInconsistencyException format:@"annotations should contains only objs conforms to MKAnnotation protocol"];
107+
checkAnnotationsCompliance(annotations, @protocol(MKAnnotation));
104108
[_annotationArray removeObjectsInArray:annotations];
105109
}
106110

@@ -165,4 +169,14 @@ - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state object
165169
return [_annotationArray countByEnumeratingWithState:state objects:buffer count:len];
166170
}
167171

172+
#pragma mark - Private / Helper methods
173+
174+
void checkAnnotationsCompliance(NSArray *annotations, Protocol *p) {
175+
for (id annotation in annotations) {
176+
if (! [annotation conformsToProtocol:p]) {
177+
[NSException raise:NSInternalInconsistencyException format:@"annotations should contains only objs conforms to MKAnnotation protocol"];
178+
}
179+
}
180+
}
181+
168182
@end

EPMapExtensions/HKMapViewDecorator.m

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,23 @@ - (id)init
3636
- (void)registerAnnotationViewForClass:(Class<MKAnnotation>)cls annotationView:(NSString *)annotation
3737
{
3838
NSParameterAssert(annotation != nil && cls != Nil);
39-
if ([NSClassFromString(annotation) isSubclassOfClass:[MKAnnotationView class]])
39+
if ([NSClassFromString(annotation) isSubclassOfClass:[MKAnnotationView class]]) {
4040
_annotationViewForClass[NSStringFromClass(cls)] = NSClassFromString(annotation);
41-
else
41+
}
42+
else {
4243
[[NSException exceptionWithName:NSInvalidArgumentException
4344
reason:[NSString stringWithFormat:@"Annotation %@ should be any subclass of the MKAnnotationView", annotation]
4445
userInfo:nil] raise];
46+
}
4547
}
4648

4749
- (void)registerAnnotationViewForClass:(Class<MKAnnotation>)cls annotationView:(NSString *)annotation
4850
configBlock:(void (^)(MKAnnotationView *, id<MKAnnotation>))block
4951
{
5052
[self registerAnnotationViewForClass:cls annotationView:annotation];
51-
if (block)
53+
if (block) {
5254
[self setConfigBlockForClass:cls block:block];
55+
}
5356
}
5457

5558
- (void)setConfigBlockForClass:(Class<MKAnnotation>)cls block:(ConfigurationBlockType)block
@@ -84,19 +87,21 @@ - (void)registerAnnotationViewForClass:(Class <MKAnnotation>)cls translationBloc
8487
}
8588
}
8689

87-
8890
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
8991
{
9092

9193
if ([annotation class] == [MKUserLocation class]) return _userLocationAnnotationView;
9294

93-
if ([self HK_isNoneRegistered])
94-
return _defaultAnnotationView;
95-
95+
if ([self HK_isNoneRegistered]) {
96+
return _defaultAnnotationView;
97+
}
98+
9699
MKAnnotationView* annotationView;
97100
NSString *reuseIdentifier = [self HK_reuseIdentifierForAnnotation:annotation];
98101

99-
if (!reuseIdentifier) return _defaultAnnotationView;
102+
if (![reuseIdentifier length]) {
103+
return _defaultAnnotationView;
104+
}
100105

101106
if( !(annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:reuseIdentifier]) ) {
102107
Class c = [self HK_annotationViewClassFromAnnotation:annotation];
@@ -113,35 +118,42 @@ - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnot
113118

114119
- (NSString *)HK_reuseIdentifierForAnnotation:(id<MKAnnotation>)annotation
115120
{
116-
if (_translationBlockForAllClasses && _translationBlockForAllClasses(annotation))
121+
if (_translationBlockForAllClasses && _translationBlockForAllClasses(annotation)) {
117122
return _translationBlockForAllClasses(annotation);
123+
}
118124

119125
NSString *annotationViewClass = NSStringFromClass([annotation class]);
120126

121-
if (_translationBlockForClass[annotationViewClass] && ((TranslationBlockType) _translationBlockForClass[annotationViewClass]) (annotation) )
127+
if (_translationBlockForClass[annotationViewClass] && ((TranslationBlockType) _translationBlockForClass[annotationViewClass]) (annotation) ) {
122128
return ((TranslationBlockType) _translationBlockForClass[annotationViewClass]) (annotation);
123-
else
129+
}
130+
else {
124131
return NSStringFromClass(_annotationViewForClass[annotationViewClass]);
132+
}
125133
}
126134

127135
- (Class)HK_annotationViewClassFromAnnotation:(id<MKAnnotation>)annotation
128136
{
129137
Class annotationViewClass = NSClassFromString([self HK_reuseIdentifierForAnnotation:annotation]);
130138

131-
if (! [annotationViewClass isSubclassOfClass:[MKAnnotationView class]] )
139+
if (! [annotationViewClass isSubclassOfClass:[MKAnnotationView class]] ) {
132140
[[NSException exceptionWithName:NSInvalidArgumentException
133141
reason:[NSString stringWithFormat:@"%@ should be any subclass of the MKAnnotationView", annotationViewClass]
134142
userInfo:nil] raise];
143+
}
135144

136145
return annotationViewClass;
137146
}
138147

139148
- (void)HK_configureAnnotationView:(MKAnnotationView *)annotationView
140149
{
141-
if ( _configBlockForAnnotationView[ NSStringFromClass([annotationView.annotation class])] )
150+
if ( _configBlockForAnnotationView[ NSStringFromClass([annotationView.annotation class])] ) {
142151
( (ConfigurationBlockType)_configBlockForAnnotationView[ NSStringFromClass([annotationView.annotation class]) ] )(annotationView, annotationView.annotation);
143-
if (_configBlockForAllClasses)
144-
_configBlockForAllClasses(annotationView, annotationView.annotation);
152+
}
153+
154+
if (_configBlockForAllClasses) {
155+
_configBlockForAllClasses(annotationView, annotationView.annotation);
156+
}
145157
}
146158

147159
- (BOOL)HK_isNoneRegistered

EPMapExtensions/HKPointOfInterest.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#import <CoreLocation/CoreLocation.h>
1111

1212
/**
13-
`EPPointOfInterest` represent an annotation object responding to `MKAnnotation` protocol
13+
`HKPointOfInterest` represent an annotation object responding to `MKAnnotation` protocol
1414
*/
1515

1616
@interface HKPointOfInterest : NSObject <MKAnnotation, NSCopying>
@@ -29,7 +29,7 @@
2929
Coordinate of the point of interest
3030
*/
3131

32-
@property (readonly) CLLocationCoordinate2D coordinate;
32+
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
3333

3434
@end
3535

EPMapExtensionsTests/HKAnnotationArrayTest.m

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,26 @@ - (void)setUp
2525
addressDictionary:nil];
2626
}
2727

28+
- (void)testInitWithAnnotationsConformingToMKAnnotationProtocol
29+
{
30+
_annotationArray = [[HKAnnotationArray alloc] initWithAnnotationArray:@[_poi, _poi]];
31+
STAssertTrue([_annotationArray count] == 2, @"annotation array obj should contain 2 pois");
32+
}
33+
34+
- (void)testInitWithAnnotationsNotConformingToMKAnnotationProtocol
35+
{
36+
STAssertThrows((_annotationArray = [[HKAnnotationArray alloc] initWithAnnotationArray:@[_poi, @"test"]]),
37+
@"should raise exception");
38+
STAssertTrue([_annotationArray count] == 0, @"annotation array should be nil");
39+
}
40+
41+
- (void)testWithNilAnnoatationsCollection
42+
{
43+
_annotationArray = [[HKAnnotationArray alloc] initWithAnnotationArray:nil];
44+
STAssertTrue(_annotationArray != nil, @"annotation array should have been allocated");
45+
STAssertTrue([_annotationArray count] == 0, @"annotation array should contain 0 elements");
46+
}
47+
2848
- (void)testAddAnnotationConformToProtocol
2949
{
3050
[_annotationArray addAnnotation:_poi];

iOSMapExtension/HKAppDelegate.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88

99
#import <UIKit/UIKit.h>
1010

11-
@class EPViewController;
11+
@class HKViewController;
1212

1313
@interface HKAppDelegate : UIResponder <UIApplicationDelegate>
1414

1515
@property (strong, nonatomic) UIWindow *window;
1616

17-
@property (strong, nonatomic) EPViewController *viewController;
17+
@property (strong, nonatomic) HKViewController *viewController;
1818

1919
@end

0 commit comments

Comments
 (0)