Skip to content

Commit 09680f7

Browse files
fmannsfacebook-github-bot
authored andcommitted
Return instancetype for shared RCTI18nUtil instance
Summary: In modern Objective-C you should use the `instancetype` keyword for methods which return an instance of the class they are called on. See Apple's [Adopting Modern Objective-C](https://developer.apple.com/library/content/releasenotes/ObjectiveC/ModernizationObjC/AdoptingModernObjective-C/AdoptingModernObjective-C.html) guide. Because `sharedInstance` was returning an object of type `id`, the returned value needed to be cast before it could be used in Swift. I also changed the implementation of `sharedInstance` to use Grand Central Dispatch, which is the generally accepted best way of creating a singleton in Objective-C. I verified my changes with the "RTLExample" app in RNTester. | LTR | RTL | |---|---| |<img width="300" src="https://user-images.githubusercontent.com/1413388/31155210-6454b4d6-a87a-11e7-9dd7-9a52f3924737.png">|<img width="300" src="https://user-images.githubusercontent.com/1413388/31155233-8702aff6-a87a-11e7-8028-51cf2b3eb0c4.png">| Closes #16196 Differential Revision: D5971898 Pulled By: shergin fbshipit-source-id: dfa375c89248adfc9fd885cacc6a6d4cbfea6e90
1 parent 59d9f8c commit 09680f7

2 files changed

Lines changed: 11 additions & 8 deletions

File tree

React/Modules/RCTI18nUtil.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@
1717
*/
1818
@interface RCTI18nUtil : NSObject
1919

20+
+ (instancetype)sharedInstance;
21+
2022
- (BOOL)isRTL;
2123
- (BOOL)isRTLAllowed;
2224
- (void)allowRTL:(BOOL)value;
2325
- (BOOL)isRTLForced;
2426
- (void)forceRTL:(BOOL)value;
25-
+ (id)sharedInstance;
2627

2728
@end

React/Modules/RCTI18nUtil.m

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@
1313

1414
@implementation RCTI18nUtil
1515

16-
+ (id)sharedInstance {
17-
static RCTI18nUtil *sharedRCTI18nUtilInstance = nil;
18-
@synchronized(self) {
19-
if (sharedRCTI18nUtilInstance == nil)
20-
sharedRCTI18nUtilInstance = [self new];
21-
}
22-
return sharedRCTI18nUtilInstance;
16+
+ (instancetype)sharedInstance
17+
{
18+
static RCTI18nUtil *sharedInstance;
19+
static dispatch_once_t onceToken;
20+
dispatch_once(&onceToken, ^{
21+
sharedInstance = [self new];
22+
});
23+
24+
return sharedInstance;
2325
}
2426

2527
/**

0 commit comments

Comments
 (0)