[ios, macos] Designated initializers
Added designated initializers to MGLStyleValue and friends. Restored custom equality and descriptions to MGLStyleFunction. Raise an exception if an unrecognized subclass of MGLStyleValue or MGLStyleValue itself is passed into a style attribute property.
This commit is contained in:
committed by
Minh Nguyễn
parent
9652028915
commit
cf5aa9c648
@@ -13,7 +13,9 @@
|
||||
|
||||
+ (instancetype)valueWithRawValue:(T)rawValue;
|
||||
|
||||
- (instancetype)initWithRawValue:(T)rawValue;
|
||||
- (instancetype)init NS_UNAVAILABLE;
|
||||
|
||||
- (instancetype)initWithRawValue:(T)rawValue NS_DESIGNATED_INITIALIZER;
|
||||
|
||||
@property (nonatomic) T rawValue;
|
||||
|
||||
@@ -22,9 +24,10 @@
|
||||
@interface MGLStyleFunction<T> : MGLStyleValue
|
||||
|
||||
+ (instancetype)functionWithBase:(CGFloat)base stops:(NSDictionary<NSNumber *, MGLStyleValue<T> *> *)stops;
|
||||
|
||||
+ (instancetype)functionWithStops:(NSDictionary<NSNumber *, MGLStyleValue<T> *> *)stops;
|
||||
|
||||
- (instancetype)initWithBase:(CGFloat)base stops:(NSDictionary<NSNumber *, MGLStyleValue<T> *> *)stops;
|
||||
- (instancetype)initWithBase:(CGFloat)base stops:(NSDictionary<NSNumber *, MGLStyleValue<T> *> *)stops NS_DESIGNATED_INITIALIZER;
|
||||
|
||||
@property (nonatomic) CGFloat base;
|
||||
@property (nonatomic, copy) NSDictionary<NSNumber *, MGLStyleValue<T> *> *stops;
|
||||
|
||||
@@ -33,6 +33,10 @@
|
||||
return [self.rawValue description];
|
||||
}
|
||||
|
||||
- (NSString *)debugDescription {
|
||||
return [self.rawValue debugDescription];
|
||||
}
|
||||
|
||||
- (BOOL)isEqual:(MGLStyleConstantValue *)other {
|
||||
return [other isKindOfClass:[self class]] && [other.rawValue isEqual:self.rawValue];
|
||||
}
|
||||
@@ -54,18 +58,31 @@
|
||||
}
|
||||
|
||||
- (instancetype)init {
|
||||
if (self = [super init]) {
|
||||
_base = 1;
|
||||
}
|
||||
return self;
|
||||
return [self initWithBase:1 stops:@{}];
|
||||
}
|
||||
|
||||
- (instancetype)initWithBase:(CGFloat)base stops:(NSDictionary *)stops {
|
||||
if (self = [self init]) {
|
||||
if (self = [super init]) {
|
||||
_base = base;
|
||||
_stops = stops;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (NSString *)description {
|
||||
return [NSString stringWithFormat:@"<%@: %p, base = %f; stops = %@>",
|
||||
NSStringFromClass([self class]), (void *)self,
|
||||
self.base,
|
||||
self.stops];
|
||||
}
|
||||
|
||||
- (BOOL)isEqual:(MGLStyleFunction *)other {
|
||||
return ([other isKindOfClass:[self class]] && other.base == self.base
|
||||
&& [other.stops isEqualToDictionary:self.stops]);
|
||||
}
|
||||
|
||||
- (NSUInteger)hash {
|
||||
return self.base + self.stops.hash;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@@ -42,6 +42,12 @@ public:
|
||||
mbglStops.emplace_back(zoomKey.floatValue, mbglStopValue.asConstant());
|
||||
}];
|
||||
return mbgl::style::Function<MBGLType>({{mbglStops}}, function.base);
|
||||
} else if (value) {
|
||||
[NSException raise:@"MGLAbstractClassException" format:
|
||||
@"The style value %@ cannot be applied to the style. "
|
||||
@"Make sure the style value was created as a member of a concrete subclass of MGLStyleValue.",
|
||||
NSStringFromClass([value class])];
|
||||
return {};
|
||||
} else {
|
||||
return {};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user