From 09680f71df6afede48cbce74628ac393c60e433f Mon Sep 17 00:00:00 2001 From: Frank Manns Date: Tue, 3 Oct 2017 19:48:38 -0700 Subject: [PATCH] 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 | |---|---| ||| Closes https://github.com/facebook/react-native/pull/16196 Differential Revision: D5971898 Pulled By: shergin fbshipit-source-id: dfa375c89248adfc9fd885cacc6a6d4cbfea6e90 --- React/Modules/RCTI18nUtil.h | 3 ++- React/Modules/RCTI18nUtil.m | 16 +++++++++------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/React/Modules/RCTI18nUtil.h b/React/Modules/RCTI18nUtil.h index b98719765..0f2287218 100644 --- a/React/Modules/RCTI18nUtil.h +++ b/React/Modules/RCTI18nUtil.h @@ -17,11 +17,12 @@ */ @interface RCTI18nUtil : NSObject ++ (instancetype)sharedInstance; + - (BOOL)isRTL; - (BOOL)isRTLAllowed; - (void)allowRTL:(BOOL)value; - (BOOL)isRTLForced; - (void)forceRTL:(BOOL)value; -+ (id)sharedInstance; @end diff --git a/React/Modules/RCTI18nUtil.m b/React/Modules/RCTI18nUtil.m index 443c99302..a3a206b23 100644 --- a/React/Modules/RCTI18nUtil.m +++ b/React/Modules/RCTI18nUtil.m @@ -13,13 +13,15 @@ @implementation RCTI18nUtil -+ (id)sharedInstance { - static RCTI18nUtil *sharedRCTI18nUtilInstance = nil; - @synchronized(self) { - if (sharedRCTI18nUtilInstance == nil) - sharedRCTI18nUtilInstance = [self new]; - } - return sharedRCTI18nUtilInstance; ++ (instancetype)sharedInstance +{ + static RCTI18nUtil *sharedInstance; + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + sharedInstance = [self new]; + }); + + return sharedInstance; } /**