简体   繁体   English

将此Java代码转换为Objective C代码的最佳方法是什么?

[英]What is the best way to convert this java code into Objective C code?

public byte[] toBytes() {
    size = 12;
    ByteBuffer buf = ByteBuffer.allocate(size);
    buf.putInt(type.ordinal());//type is a enum
    buf.putInt(id);
    buf.putInt(size);
    return buf.array();
}

@Override
public void fromBytes(byte[] data) {
    ByteBuffer buf = ByteBuffer.allocate(data.length);
    buf.put(data);
    buf.rewind();
    type = MessageType.values()[buf.getInt()];
    id = buf.getInt();
    size = buf.getInt();
}

I have two java methods and want to write an objective C method.. For the first method I wrote it into an Objective C code like 我有两个java方法,并想编写一个目标C方法。对于第一个方法,我将其编写为一个Objective C代码,例如

- (NSMutableData *) toBytes{
    size = 12;

    NSMutableData *buf = [[NSMutableData alloc] initWithCapacity:size];

    NSData *dataType = [NSData dataWithBytes: &type length: sizeof(type)];
    NSData *dataId = [NSData dataWithBytes: &msgId length: sizeof(msgId)];
    NSData *dataSize = [NSData dataWithBytes: &size length: sizeof(size)];

    [buf appendData:dataType];
    [buf appendData:dataId];
    [buf appendData:dataSize];

    [dataType release];
    [dataId release];
    [dataSize release];

    return buf;
}

But not sure how to read it back... It could've been easier if I add only one data into the buffer but I added total three data so I don't know how to read those back.. Thanks in advance... 但是不确定如何回读...如果只向缓冲区中添加一个数据,但总共添加了三个数据,这样会更容易,所以我不知道如何回读这些数据。 。

Note to LCYSoft: i'm making this a community wiki. LCYSoft注意:我正在将其作为社区Wiki。 please correct any issues. 请更正任何问题。 i didn't compile this. 我没有编译这个。 since you posted one direction and really want an answer, i provided one. 因为您发布了一个方向并且真的想要一个答案,所以我提供了一个方向。 sorry, i am kinda busy atm. 抱歉,我有点忙。

this demonstrates both directions, and expands on the OP: 这演示了两个方向,并在OP上进行了扩展:

typedef enum t_mon_enum_type {
  MONEnum_Edno = 1,
  MONEnum_Dve = 2,
  MONEnum_Tre = 3
} t_mon_enum_type;

@interface MONObject : NSObject
{
    t_mon_enum_type type;
    int msgId;
    int size;
}

@end

@implementation MONObject

/* ... */

- (NSMutableData *)dataRepresentation
{
    const int typeAsInt = (int)type;
    const size_t capacity = sizeof(typeAsInt) + sizeof(msgId) + sizeof(size);
    NSMutableData * data = [[NSMutableData alloc] initWithCapacity:capacity];

    [data appendBytes:&typeAsInt length:sizeof(typeAsInt)];
    [data appendBytes:&msgId length:sizeof(msgId)];
    [data appendBytes:&size length:sizeof(size)];

    return [data autorelease];
}

- (BOOL)isDataRepresentationValid:(NSData *)data { /* @todo */ }

- (BOOL)restoreFromDataRepresentation:(NSData *)data
{
    if (![self isDataRepresentationValid]) {
        return NO;
    }

    NSRange range = { 0, 0 };

    int tmp = 0;
    /* restore `type` */
    range.length = sizeof(tmp);
    [data getBytes:&tmp range:range];
    type = (t_mon_enum_type)tmp;
    /* advance read position */
    range.location += range.length;
    /* restore `msgId` */
    range.length = sizeof(msgId);
    [data getBytes:&msgId range:range];
    /* advance read position */
    range.location += range.length;
    /*
       setting the length here is redundant in this case, but it's how we
       write it when dealing with more complex pod types.
     */
    range.length = sizeof(size);
    [data getBytes:&size range:range];

    return YES;
}

i'm not going to rewrite the program for you, but i'll provide a tip: 我不会为您重写程序,但我将提供一个提示:

you can use c++ in objc programs. 您可以在objc程序中使用c ++。 specifically, you can compile as C (.c), ObjC (.m), C++ (.cpp), and ObjC++ (.mm). 具体来说,您可以编译为C(.c),ObjC(.m),C ++(.cpp)和ObjC ++(.mm)。 note: one common extension follows each language. 注意:每种语言都有一个通用的扩展名。 the compiler will (by default) compile using the language implied by the file extension. (默认情况下)编译器将使用文件扩展名所隐含的语言进行编译。

now, many java programs more closely resemble c++ programs. 现在,许多Java程序与c ++程序更加相似。 if you're porting a program, also consider writing it in c++ since the program will often be closer to the java variant. 如果要移植程序,还应考虑用c ++编写它,因为该程序通常更接近于Java变体。

for objc, you'd probably use CF/NS-MutableData 对于objc,您可能会使用CF/NS-MutableData

for c++, you can use std::vector 对于C ++,您可以使用std::vector

good luck 祝好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM