0

我将 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")

任何帮助将不胜感激。

4

2 回答 2

1

mResponseData_是一个 ivar 并且 Swift 只能访问 Objective-c 的propertiesfunctions。或者,您可以使用mTest.valueForKey("mResponseData_")mTest.setValue(someValue, forKey: "mResponseData_")/或编辑生成的类并在属性或方法中转换 ivar。

并不是说您可以使用计算的 get 和 set 创建扩展文件来封装valueForKey实现。

您可以参考这个问题了解更多详情。

于 2016-04-21T00:47:56.670 回答
1

我有限的理解是 Swift 将 Objective C 属性作为字段导入,而不是字段本身。因此,您需要 mResponseData 的访问器方法。

好消息是 j2objc 有一个Property annotation,您可以将其添加到 mResponseDate 字段中,以便在翻译期间生成等效的 Objective C 属性。该属性应该稍后在 Swift 中导入。

Property 注释还将生成默认访问器(就像在 Objective C 中的 @synthesize 一样),因此如果您的 Java 类已经有访问器,请使用 getter= 和 setter= @property 属性指定它们。翻译器的testProperties() 测试演示了一个复杂的属性注释示例。

于 2016-04-20T03:21:52.350 回答