我有一些看起来像这样的旧 C 代码:
#include <stdio.h>
#include <strings.h>
main()
{
FILE *output;
struct socket_cpacket
{
char type; /* CP_SOCKET */
char version;
char udp_version; /* was pad2 */
char pad3;
unsigned socket;
};
struct socket_cpacket sockPack;
bzero(&sockPack,sizeof(sockPack));
sockPack.type = 27;
sockPack.version = 4;
sockPack.udp_version = 10;
sockPack.pad3 = 0;
sockPack.socket = 0;
output = fopen("/tmp/sockPack.bin", "wb");
fwrite(&sockPack, sizeof(sockPack), 1, output);
}
我想在 obj-c 中复制这个功能,并开始使用 NSCoding 协议。
CP_Socket.h
#import <Foundation/Foundation.h>
@interface CP_Socket : NSObject <NSCoding>
{
@private
char type;
char version;
char udp_version;
char pad3;
unsigned int socket;
}
@property (readonly) char type;
@property (readonly) char version;
@property (readonly) char udp_version;
@property (readonly) char pad3;
@property unsigned int socket;
typedef enum {
mTYPE = 27,
mVERSION = 4,
mUDP_VERSION = 10,
} cpSocketEnum;
@end
和 CP_Socket.m
#import "CP_Socket.h"
@implementation CP_Socket
#pragma mark ======== properties =========
@synthesize type;
@synthesize version;
@synthesize udp_version;
@synthesize pad3;
@synthesize socket;
- (id)init {
NSLog(@"init");
if( !( self = [super init] ) )
return nil;
type = mTYPE;
version = mVERSION;
udp_version = mUDP_VERSION;
pad3 = 0;
socket = 0;
return self;
}
#pragma mark ======== Archiving and unarchiving methods =========
//
// Archives and Serializations Programming Guide for Cocoa
// http://bit.ly/PAaRsV
//
// NSCoding Protocol Reference
// http://bit.ly/PAb1Rd
//
- (void)encodeWithCoder:(NSCoder *)coder
{
NSLog(@"encodeWithCoder");
[coder encodeBytes:[self type] length:1 forKey:@"type"];
//[coder encodeBytes:[self version] length:1 forKey:@"version"];
//[coder encodeBytes:[self udp_version] length:1 forKey:@"udp_version"];
//[coder encodeBytes:[self pad3] length:1 forKey:@"pad3"];
//[coder encodeInt:[self socket] forKey:@"socket"];
}
- (id)initWithCoder:(NSCoder *)coder
{
NSLog(@"initWithCoder");
}
@end
第一个问题,[coder encodeBytes:[self type] length:1 forKey:@"type"]; 发出警告。不兼容的整数到指针转换将“char”发送到“const uint8_t *”类型的参数。
如何编码一个字符?
我试过 [coder encodeInt:[self type] forKey:@"type"]; 但是字符!= int。
与代码一起进一步了解它是如何工作的;obj-c 代码生成的文件是 280 字节,查看文件内部我看到看起来像名称损坏的类标识符。
我已经尝试过 NSKeyedArchiver 和 NSArchiver ,结果相同。
我不知道从这里去哪里。C 代码生成一个 8 字节的文件。我希望 obj-c 代码在使用诸如 NSCoding 协议之类的一些面向对象的东西时做同样的事情。
我觉得我将不得不扩展 NSCoder 对象来完成这项工作。
任何帮助,将不胜感激。