J2Objc,Protobuf nano和Swift

时间:2016-04-20 01:35:51

标签: android protocol-buffers j2objc

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

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分:1)

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

并非您可以使用computed get和set创建扩展文件来封装valueForKey实现。

您可以参考此question了解更多详情。