我怎样才能编写与此Java代码等效的Objective-C?
public static @Nullable byte[] getUnknownData(Sample s){
int length = s.data.length-index;
byte data[] = new byte[length];
for(int i=0;i<length;i++){
data[i]=s.data[UNKNOWN_STATUS_INDEX+i].byteValue();
}
return data;
}
Objective-C代码:
- (NSData *)getUnknownData:(Sample s) {
int length = s.data.count-index;
NSData * data = [NSData alloc]init;
. . . ????
}
答案 0 :(得分:1)
您应该使用NSMutableData
,它是NSData
的可修改子类:
-(NSData *)getUnknownData:(Sample s){
int length = s.data.count-index;
NSMutableData *theData = [NSMutableData dataWithCapacity:length];
theData.length = length;
char *theBytes = (char *)theData.mutableBytes;
for(int i = 0; i < length; ++i) {
theBytes[i] = (char)[s.data[UNKNOWN_STATUS_INDEX + i] intValue]
}
return [theData copy];
}
在保留所需的字节数后,您必须将数组中的每个值转换为数据对象。