Skip to content
This repository has been archived by the owner on Jun 21, 2023. It is now read-only.

Commit

Permalink
Update nullability; Defend against nil languageCode
Browse files Browse the repository at this point in the history
  • Loading branch information
macdrevx committed Aug 20, 2021
1 parent 9d230b2 commit f1177c1
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 7 deletions.
4 changes: 2 additions & 2 deletions platform/darwin/src/NSString+MGLAdditions.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ NS_ASSUME_NONNULL_BEGIN

/**
Returns a transliterated representation of the receiver using the specified
script. If transliteration fails, the receiver will be returned.
script. If transliteration fails or script is nil, the receiver will be returned.
Only supports scripts for languages used by Mapbox Streets.
@param script The four-letter code representing the name of the script, as
specified by ISO 15924.
*/
- (NSString *)mgl_stringByTransliteratingIntoScript:(NSString *)script;
- (NSString *)mgl_stringByTransliteratingIntoScript:(nullable NSString *)script;

@end

Expand Down
2 changes: 1 addition & 1 deletion platform/darwin/src/NSString+MGLAdditions.m
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ - (NSString *)mgl_titleCasedStringWithLocale:(NSLocale *)locale {
return string;
}

- (NSString *)mgl_stringByTransliteratingIntoScript:(NSString *)script {
- (NSString *)mgl_stringByTransliteratingIntoScript:(nullable NSString *)script {
NSMutableString *string = self.mutableCopy;
NSStringTransform transform;
if ([script isEqualToString:@"Latn"]) {
Expand Down
2 changes: 2 additions & 0 deletions platform/darwin/test/MGLNSStringAdditionsTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ - (void)testTransliteratedString {
XCTAssertEqualObjects([@"ロンドン" mgl_stringByTransliteratingIntoScript:@"Jpan"], @"ロンドン");
XCTAssertEqualObjects([@"ロンドン" mgl_stringByTransliteratingIntoScript:@"Kore"], @"론돈");
XCTAssertEqualObjects([@"ロンドン" mgl_stringByTransliteratingIntoScript:@"Fake"], @"ロンドン");

XCTAssertEqualObjects([@"" mgl_stringByTransliteratingIntoScript:nil], @"");
}

@end
11 changes: 7 additions & 4 deletions platform/ios/src/MGLMapAccessibilityElement.mm
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,11 @@ - (instancetype)initWithAccessibilityContainer:(id)container feature:(id<MGLFeat
_feature = feature;

NSString *languageCode = [MGLVectorTileSource preferredMapboxStreetsLanguage];
NSString *nameAttribute = [NSString stringWithFormat:@"name_%@", languageCode];
NSString *name = [feature attributeForKey:nameAttribute];
NSString *name;
if (languageCode != nil) {
NSString *nameAttribute = [NSString stringWithFormat:@"name_%@", languageCode];
name = [feature attributeForKey:nameAttribute];
}

NSString *dominantScript;
if (name == nil && [feature attributeForKey:@"name"] != nil) {
Expand All @@ -62,7 +65,7 @@ - (instancetype)initWithAccessibilityContainer:(id)container feature:(id<MGLFeat
// may be in the local language, which may be written in another script.
// Attempt to transform to the script of the preferred language, keeping
// the original string if no transform exists or if transformation fails.
if (!dominantScript) {
if (dominantScript == nil && languageCode != nil) {
dominantScript = [NSOrthography mgl_dominantScriptForMapboxStreetsLanguage:languageCode];
}
name = [name mgl_stringByTransliteratingIntoScript:dominantScript];
Expand All @@ -88,7 +91,7 @@ - (instancetype)initWithAccessibilityContainer:(id)container feature:(id<MGLFeat
// Announce the kind of place or POI.
NSString *languageCode = [MGLVectorTileSource preferredMapboxStreetsLanguage];
NSString *categoryAttribute = [NSString stringWithFormat:@"category_%@", languageCode];
if (attributes[categoryAttribute]) {
if (languageCode != nil && attributes[categoryAttribute]) {
[facts addObject:attributes[categoryAttribute]];
} else if (attributes[@"type"]) {
// FIXME: Unfortunately, these types aren’t a closed set that can be
Expand Down

0 comments on commit f1177c1

Please sign in to comment.