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 https://github.com/facebook/react-native/pull/16196

Differential Revision: D5971898

Pulled By: shergin

fbshipit-source-id: dfa375c89248adfc9fd885cacc6a6d4cbfea6e90
This commit is contained in:
Frank Manns 2017-10-03 19:48:38 -07:00 committed by Facebook Github Bot
parent 59d9f8ca5e
commit 09680f71df
2 changed files with 11 additions and 8 deletions

View File

@ -17,11 +17,12 @@
*/ */
@interface RCTI18nUtil : NSObject @interface RCTI18nUtil : NSObject
+ (instancetype)sharedInstance;
- (BOOL)isRTL; - (BOOL)isRTL;
- (BOOL)isRTLAllowed; - (BOOL)isRTLAllowed;
- (void)allowRTL:(BOOL)value; - (void)allowRTL:(BOOL)value;
- (BOOL)isRTLForced; - (BOOL)isRTLForced;
- (void)forceRTL:(BOOL)value; - (void)forceRTL:(BOOL)value;
+ (id)sharedInstance;
@end @end

View File

@ -13,13 +13,15 @@
@implementation RCTI18nUtil @implementation RCTI18nUtil
+ (id)sharedInstance { + (instancetype)sharedInstance
static RCTI18nUtil *sharedRCTI18nUtilInstance = nil; {
@synchronized(self) { static RCTI18nUtil *sharedInstance;
if (sharedRCTI18nUtilInstance == nil) static dispatch_once_t onceToken;
sharedRCTI18nUtilInstance = [self new]; dispatch_once(&onceToken, ^{
} sharedInstance = [self new];
return sharedRCTI18nUtilInstance; });
return sharedInstance;
} }
/** /**