Skip to content

Commit

Permalink
Return instancetype for shared RCTI18nUtil instance
Browse files Browse the repository at this point in the history
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
  • Loading branch information
fmanns authored and facebook-github-bot committed Oct 4, 2017
1 parent 59d9f8c commit 09680f7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
3 changes: 2 additions & 1 deletion React/Modules/RCTI18nUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 9 additions & 7 deletions React/Modules/RCTI18nUtil.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down

0 comments on commit 09680f7

Please sign in to comment.