我将 Java 类翻译为 Objective C(使用 cradle 插件的 J2Objc)。简单的 Android 应用程序和单元测试工作正常。同样简单的 Objective C iOS 应用程序也可以完美运行。但是,当我尝试在 Swift 项目中编写类似的代码时,我无法访问实例变量、查找方法等。这对我来说相对较新,所以我可能会遗漏一些基本的东西。
这是Objective C中的代码。完美运行:
SHRRefreshAppointments *mTest = [[SHRRefreshAppointments alloc] initWithId:nil withBoolean:true];
[mTest run];
SHREventListResp *mResp = [SHREventListResp parseFromWithByteArray:mTest->mResponseData_];
NSLog(@"%@", mResp.description);
Swift 中的相同代码 - 我找不到在 Objective C 中现成的实例变量 mResponseData:
let mTest : SHRRefreshAppointments = SHRRefreshAppointments(id: nil, withBoolean: true)
mTest.run()
var mResp : SHREventListResp = SHREventListResp.parseFromWithByteArray(mTest.)
以下是桥接头的内容:
#include "JreEmulation.h"
#include "J2ObjC_header.h"
#import "com/gcatconsult/shared/remote/AppConstants.h"
#import "com/gcatconsult/shared/remote/AppUtils.h"
#import "com/gcatconsult/shared/messages/nano/Resp.h"
#import "com/gcatconsult/shared/messages/nano//Req.h"
#import "com/gcatconsult/shared/messages/nano/EventListResp.h"
#import "com/gcatconsult/shared/messages/nano/EventListReq.h"
#import "com/gcatconsult/shared/messages/nano/Event.h"
#import "com/gcatconsult/shared/remote/RefreshAppointments.h"
#import "com/gcatconsult/shared/remote/NetworkBase.h"
SHRRefreshAppointments 标题:
//
// Generated by the J2ObjC translator. DO NOT EDIT!
// source: /Users/gabrielchoza/AndroidStudioProjects/GcatMobile/shared/src/main/java/com/gcatconsult/shared/remote/RefreshAppointments.java
//
#include "J2ObjC_header.h"
#pragma push_macro("INCLUDE_ALL_ComGcatconsultSharedRemoteRefreshAppointments")
#ifdef RESTRICT_ComGcatconsultSharedRemoteRefreshAppointments
#define INCLUDE_ALL_ComGcatconsultSharedRemoteRefreshAppointments 0
#else
#define INCLUDE_ALL_ComGcatconsultSharedRemoteRefreshAppointments 1
#endif
#undef RESTRICT_ComGcatconsultSharedRemoteRefreshAppointments
#if !defined (SHRRefreshAppointments_) && (INCLUDE_ALL_ComGcatconsultSharedRemoteRefreshAppointments || defined(INCLUDE_SHRRefreshAppointments))
#define SHRRefreshAppointments_
#define RESTRICT_ComGcatconsultSharedRemoteNetworkBase 1
#define INCLUDE_SHRNetworkBase 1
#include "com/gcatconsult/shared/remote/NetworkBase.h"
#define RESTRICT_JavaLangRunnable 1
#define INCLUDE_JavaLangRunnable 1
#include "java/lang/Runnable.h"
@interface SHRRefreshAppointments : SHRNetworkBase < JavaLangRunnable >
#pragma mark Public
- (instancetype)initWithId:(id)requestData
withBoolean:(jboolean)asyncCall;
#pragma mark Protected
- (void)postProcessExcecute;
@end
J2OBJC_EMPTY_STATIC_INIT(SHRRefreshAppointments)
FOUNDATION_EXPORT void SHRRefreshAppointments_initWithId_withBoolean_(SHRRefreshAppointments *self, id requestData, jboolean asyncCall);
FOUNDATION_EXPORT SHRRefreshAppointments *new_SHRRefreshAppointments_initWithId_withBoolean_(id requestData, jboolean asyncCall) NS_RETURNS_RETAINED;
FOUNDATION_EXPORT SHRRefreshAppointments *create_SHRRefreshAppointments_initWithId_withBoolean_(id requestData, jboolean asyncCall);
J2OBJC_TYPE_LITERAL_HEADER(SHRRefreshAppointments)
@compatibility_alias ComGcatconsultSharedRemoteRefreshAppointments SHRRefreshAppointments;
#endif
#pragma pop_macro("INCLUDE_ALL_ComGcatconsultSharedRemoteRefreshAppointments")
SHRNetworkBase 超类:
//
// Generated by the J2ObjC translator. DO NOT EDIT!
// source: /Users/gabrielchoza/AndroidStudioProjects/GcatMobile/shared/src/main/java/com/gcatconsult/shared/remote/NetworkBase.java
//
#include "J2ObjC_header.h"
#pragma push_macro("INCLUDE_ALL_ComGcatconsultSharedRemoteNetworkBase")
#ifdef RESTRICT_ComGcatconsultSharedRemoteNetworkBase
#define INCLUDE_ALL_ComGcatconsultSharedRemoteNetworkBase 0
#else
#define INCLUDE_ALL_ComGcatconsultSharedRemoteNetworkBase 1
#endif
#undef RESTRICT_ComGcatconsultSharedRemoteNetworkBase
#if !defined (SHRNetworkBase_) && (INCLUDE_ALL_ComGcatconsultSharedRemoteNetworkBase || defined(INCLUDE_SHRNetworkBase))
#define SHRNetworkBase_
#define RESTRICT_JavaLangRunnable 1
#define INCLUDE_JavaLangRunnable 1
#include "java/lang/Runnable.h"
@class IOSByteArray;
@class JavaLangInteger;
@interface SHRNetworkBase : NSObject < JavaLangRunnable > {
@public
NSString *mGetPatientPath_;
NSString *mStringURL_;
IOSByteArray *mRequestData_;
IOSByteArray *mResponseData_;
jboolean mShouldCompress_;
jint mCurrentCall_;
jboolean mAsyncCall_;
}
#pragma mark Public
- (instancetype)initWithNSString:(NSString *)callPath
withBoolean:(jboolean)shouldCompress
withJavaLangInteger:(JavaLangInteger *)currentCall
withBoolean:(jboolean)async;
- (void)run;
#pragma mark Protected
- (void)postProcessExcecute;
- (id)sendAsyncServerRequestWithNSString:(NSString *)stringURL
withId:(id)requestData
withBoolean:(jboolean)shouldCompress
withJavaLangInteger:(JavaLangInteger *)currentCall;
@end
J2OBJC_EMPTY_STATIC_INIT(SHRNetworkBase)
J2OBJC_FIELD_SETTER(SHRNetworkBase, mGetPatientPath_, NSString *)
J2OBJC_FIELD_SETTER(SHRNetworkBase, mStringURL_, NSString *)
J2OBJC_FIELD_SETTER(SHRNetworkBase, mRequestData_, IOSByteArray *)
J2OBJC_FIELD_SETTER(SHRNetworkBase, mResponseData_, IOSByteArray *)
FOUNDATION_EXPORT void SHRNetworkBase_initWithNSString_withBoolean_withJavaLangInteger_withBoolean_(SHRNetworkBase *self, NSString *callPath, jboolean shouldCompress, JavaLangInteger *currentCall, jboolean async);
FOUNDATION_EXPORT SHRNetworkBase *new_SHRNetworkBase_initWithNSString_withBoolean_withJavaLangInteger_withBoolean_(NSString *callPath, jboolean shouldCompress, JavaLangInteger *currentCall, jboolean async) NS_RETURNS_RETAINED;
FOUNDATION_EXPORT SHRNetworkBase *create_SHRNetworkBase_initWithNSString_withBoolean_withJavaLangInteger_withBoolean_(NSString *callPath, jboolean shouldCompress, JavaLangInteger *currentCall, jboolean async);
J2OBJC_TYPE_LITERAL_HEADER(SHRNetworkBase)
@compatibility_alias ComGcatconsultSharedRemoteNetworkBase SHRNetworkBase;
#endif
#pragma pop_macro("INCLUDE_ALL_ComGcatconsultSharedRemoteNetworkBase")
任何帮助将不胜感激。