@@ -20,7 +20,29 @@ @implementation LMAddress
20
20
@synthesize country;
21
21
@synthesize formattedAddress;
22
22
@synthesize isValid;
23
- @synthesize ISOcountryCode;
23
+ @synthesize countryCode;
24
+
25
+ #pragma mark - CONSTANTS
26
+
27
+ // @Double
28
+ static NSString *const LMLatitudeKey = @" latitude" ;
29
+ static NSString *const LMLongitudeKey = @" longitude" ;
30
+ // @BOOL
31
+ static NSString *const LMIsValidKey = @" isValid" ;
32
+ // NSString
33
+ static NSString *const LMStreetNumberKey = @" streetNumber" ;
34
+ static NSString *const LMRouteKey = @" route" ;
35
+ static NSString *const LMLocalityKey = @" locality" ;
36
+ static NSString *const LMSubLocalityKey = @" subLocality" ;
37
+ static NSString *const LMAdministrativeAreaKey = @" administrativeArea" ;
38
+ static NSString *const LMPostalCodeKey = @" postalCode" ;
39
+ static NSString *const LMCountryKey = @" country" ;
40
+ static NSString *const LMFormattedAddressKey = @" formattedAddress" ;
41
+ static NSString *const LMCountryCodeKey = @" countryCode" ;
42
+
43
+ #define allStringKeys @[LMStreetNumberKey, LMRouteKey, LMLocalityKey, LMSubLocalityKey, \
44
+ LMAdministrativeAreaKey, LMPostalCodeKey, LMCountryKey, \
45
+ LMFormattedAddressKey, LMCountryCodeKey]
24
46
25
47
#pragma mark - INIT
26
48
@@ -47,6 +69,83 @@ - (id)initWithLocationData:(id)locationData forServiceType:(int)serviceType
47
69
return self;
48
70
}
49
71
72
+ #pragma mark - EQUALITY
73
+
74
+ - (BOOL )isEqual : (id )object
75
+ {
76
+ BOOL equal = [super isEqual: object];
77
+ if (equal)
78
+ {
79
+ return YES ;
80
+ }
81
+ if ([object isKindOfClass: [self class ]] == NO )
82
+ {
83
+ return NO ;
84
+ }
85
+ LMAddress *other = object;
86
+ // Lat/Long
87
+ equal = (self.coordinate .latitude == other.coordinate .latitude );
88
+ equal &= (self.coordinate .longitude == other.coordinate .longitude );
89
+ // IsValid
90
+ equal &= (self.isValid == other.isValid );
91
+ // The rest
92
+ for (NSString *key in allStringKeys)
93
+ {
94
+ equal &= [[self valueForKey: key] isEqual: [other valueForKey: key]];
95
+ }
96
+ return equal;
97
+ }
98
+
99
+ - (NSUInteger )hash
100
+ {
101
+ // Should be enough to hash-table well
102
+ NSUInteger hashValue = (self.isValid ? 1 : 0 );
103
+ hashValue += floor (self.coordinate .latitude ) + floor (self.coordinate .longitude );
104
+ hashValue += self.formattedAddress .hash ;
105
+ return hashValue;
106
+ }
107
+
108
+ #pragma mark - NSCODING
109
+
110
+ - (id )initWithCoder : (NSCoder *)aDecoder
111
+ {
112
+ self = [self init ];
113
+ if (self)
114
+ {
115
+ // Load doubles into coordinate
116
+ self.coordinate = CLLocationCoordinate2DMake ([aDecoder decodeDoubleForKey: LMLatitudeKey],
117
+ [aDecoder decodeDoubleForKey: LMLongitudeKey]);
118
+ // Load bool
119
+ self.isValid = [aDecoder decodeBoolForKey: LMIsValidKey];
120
+ // Load the strings into properties by name
121
+ for (NSString *key in allStringKeys)
122
+ {
123
+ [self setValue: [aDecoder decodeObjectForKey: key] forKey: key];
124
+ }
125
+ }
126
+ return self;
127
+ }
128
+
129
+ - (void )encodeWithCoder : (NSCoder *)aCoder
130
+ {
131
+ // Double
132
+ [aCoder encodeDouble: self .coordinate.latitude forKey: LMLatitudeKey];
133
+ [aCoder encodeDouble: self .coordinate.longitude forKey: LMLongitudeKey];
134
+ // Bool
135
+ [aCoder encodeBool: self .isValid forKey: LMIsValidKey];
136
+ // String
137
+ for (NSString *key in allStringKeys)
138
+ {
139
+ [aCoder encodeObject: [self valueForKey: key] forKey: key];
140
+ }
141
+ }
142
+
143
+ #pragma mark - NSCOPYING
144
+
145
+ - (id )copyWithZone : (NSZone *)zone
146
+ {
147
+ return [NSKeyedUnarchiver unarchiveObjectWithData: [NSKeyedArchiver archivedDataWithRootObject: self ]];
148
+ }
50
149
51
150
#pragma mark - PARSING
52
151
@@ -67,7 +166,7 @@ - (void)setAppleLocationData:(id)locationData
67
166
self.administrativeArea = placemark.administrativeArea ;
68
167
self.postalCode = placemark.postalCode ;
69
168
self.country = placemark.country ;
70
- self.ISOcountryCode = placemark.ISOcountryCode ;
169
+ self.countryCode = placemark.ISOcountryCode ;
71
170
self.formattedAddress = [lines componentsJoinedByString: @" , " ];
72
171
}
73
172
else
@@ -99,7 +198,7 @@ - (void)setGoogleLocationData:(id)locationData
99
198
self.administrativeArea = [self component: @" administrative_area_level_1" inArray: addressComponents ofType: @" long_name" ];
100
199
self.postalCode = [self component: @" postal_code" inArray: addressComponents ofType: @" short_name" ];
101
200
self.country = [self component: @" country" inArray: addressComponents ofType: @" long_name" ];
102
- self.ISOcountryCode = [self component: @" country" inArray: addressComponents ofType: @" short_name" ];
201
+ self.countryCode = [self component: @" country" inArray: addressComponents ofType: @" short_name" ];
103
202
self.formattedAddress = formattedAddrs;
104
203
}
105
204
else
0 commit comments