React Native集成CodePush时AppDelegate.m报语法错误求助
React Native 0.68.0 + Xcode 13 集成CodePush报错:
Expected ';' after method prototype 问题描述
我在React Native 0.68.0、Xcode 13环境下,按照CodePush官方文档给AppDelegate.m添加了CodePush相关的sourceURLForBridge:方法代码,修改后的文件内容如下:
#import <React/RCTBridgeDelegate.h> #import <UIKit/UIKit.h> #import <CodePush/CodePush.h> /* for rn codepush */ @interface AppDelegate : UIResponder <UIApplicationDelegate, RCTBridgeDelegate> @property (nonatomic, strong) UIWindow *window; /* for rn code push */ - (NSURL *)sourceURLForBridge:(RCTBridge *)bridge { #if DEBUG return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil]; #else return [CodePush bundleURL]; #endif } @end
但Xcode报错:Expected ';' after method prototype,我对iOS开发不熟悉,请问问题出在哪?
解决方案
问题核心是你把方法的实现代码写到了@interface区块里——在Objective-C中,@interface只能放方法的声明(也就是方法原型,结尾要加;),而方法的具体实现必须放到@implementation区块中。
正确的AppDelegate.m写法如下:
#import <React/RCTBridgeDelegate.h> #import <UIKit/UIKit.h> #import <CodePush/CodePush.h> /* for rn codepush */ @interface AppDelegate : UIResponder <UIApplicationDelegate, RCTBridgeDelegate> @property (nonatomic, strong) UIWindow *window; // 这里只放方法声明(原型),结尾必须加; - (NSURL *)sourceURLForBridge:(RCTBridge *)bridge; @end @implementation AppDelegate // 方法的具体实现必须放到@implementation区块内 - (NSURL *)sourceURLForBridge:(RCTBridge *)bridge { #if DEBUG return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil]; #else return [CodePush bundleURL]; #endif } // 系统自动生成的其他AppDelegate方法(比如application:didFinishLaunchingWithOptions:等)也都要放在@implementation里 @end
简单来说,你之前的代码把本该在@implementation里的方法实现,错误地放到了@interface中,导致Xcode认为你只写了方法原型却没加;,因此触发报错。
内容的提问来源于stack exchange,提问作者user938363
相关产品推荐
相关产品推荐

