Skip to content

Commit

Permalink
Use Objective-C nullability to provide better Swift types in RCTAppDe…
Browse files Browse the repository at this point in the history
…legate (#42236)

Summary:
This PR adds nullable type annotations + nullability audit regions (`NS_ASSUME_NONNULL_BEGIN`/`NS_ASSUME_NONNULL_END`) to provide better Swift types.

Before:

```swift
class AppDelegate: RCTAppDelegate {
  override func sourceURL(for bridge: RCTBridge!) -> URL! {
    self.bundleURL()
  }

  override func bundleURL() -> URL! {
    RCTBundleURLProvider.sharedSettings().jsBundleURL(forBundleRoot: "js/RNTesterApp.ios")
  }

  override func customize(_ rootView: RCTRootView!) {
    rootView.backgroundColor = .red
  }
}
```

After:

```swift
class AppDelegate: RCTAppDelegate {
  override func sourceURL(for bridge: RCTBridge) -> URL {
    self.bundleURL()
  }

  override func bundleURL() -> URL {
    RCTBundleURLProvider.sharedSettings().jsBundleURL(forBundleRoot: "js/RNTesterApp.ios")
  }

  override func customize(_ rootView: RCTRootView) {
    rootView.backgroundColor = .red
  }
}
```

## Changelog:

[IOS] [ADDED] - Provide better Swift types for RCTAppDelegate and RCTBridgeDelegate

Pull Request resolved: #42236

Test Plan:
In Xcode click the top left square button and look up the generated interface:
![CleanShot 2024-01-10 at 16 40 51@2x](https://github.com/facebook/react-native/assets/52801365/ad2ac9ed-9d9a-4c5e-a200-1d7a2802e701)

Reviewed By: christophpurrer

Differential Revision: D52660647

Pulled By: cipolleschi

fbshipit-source-id: d4d3c36b3f420b3490145c491cd3781613f4b111
  • Loading branch information
okwasniewski authored and facebook-github-bot committed Jan 11, 2024
1 parent 3ea0598 commit a7c5c28
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
12 changes: 8 additions & 4 deletions packages/react-native/Libraries/AppDelegate/RCTAppDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
@class RCTRootView;
@class RCTSurfacePresenterBridgeAdapter;

NS_ASSUME_NONNULL_BEGIN

/**
* The RCTAppDelegate is an utility class that implements some base configurations for all the React Native apps.
* It is not mandatory to use it, but it could simplify your AppDelegate code.
Expand Down Expand Up @@ -55,10 +57,10 @@
@interface RCTAppDelegate : UIResponder <UIApplicationDelegate, UISceneDelegate, RCTBridgeDelegate>

/// The window object, used to render the UViewControllers
@property (nonatomic, strong) UIWindow *window;
@property (nonatomic, strong) RCTBridge *bridge;
@property (nonatomic, strong) NSString *moduleName;
@property (nonatomic, strong) NSDictionary *initialProps;
@property (nonatomic, strong, nonnull) UIWindow *window;
@property (nonatomic, strong, nullable) RCTBridge *bridge;
@property (nonatomic, strong, nullable) NSString *moduleName;
@property (nonatomic, strong, nullable) NSDictionary *initialProps;

/**
* It creates a `RCTBridge` using a delegate and some launch options.
Expand Down Expand Up @@ -160,3 +162,5 @@
- (NSURL *)bundleURL;

@end

NS_ASSUME_NONNULL_END
4 changes: 4 additions & 0 deletions packages/react-native/React/Base/RCTBridgeDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
@class RCTBridge;
@protocol RCTBridgeModule;

NS_ASSUME_NONNULL_BEGIN

@protocol RCTBridgeDelegate <NSObject>

/**
Expand Down Expand Up @@ -77,3 +79,5 @@
- (NSDictionary<NSString *, Class> *)extraLazyModuleClassesForBridge:(RCTBridge *)bridge;

@end

NS_ASSUME_NONNULL_END

0 comments on commit a7c5c28

Please sign in to comment.