Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 如何使用initwithCoder和encodewithCoder获取指针的值_Objective C_Pointers_Nscoding - Fatal编程技术网

Objective c 如何使用initwithCoder和encodewithCoder获取指针的值

Objective c 如何使用initwithCoder和encodewithCoder获取指针的值,objective-c,pointers,nscoding,Objective C,Pointers,Nscoding,看起来我的initwithCoder和EncodWithCoder正在工作,但我正在做一些愚蠢的事情 为了简单起见,我展示了我的问题的相关代码。我有几个对象从指针和长度获取NSData,因此这些值被存储为常量字符*和长度 以下是界面: @interface IDImage : NSObject <NSCoding> @property (nonatomic) const char *templateData; @property (nonatomic) NSUInteger

看起来我的initwithCoder和EncodWithCoder正在工作,但我正在做一些愚蠢的事情

为了简单起见,我展示了我的问题的相关代码。我有几个对象从指针和长度获取NSData,因此这些值被存储为常量字符*和长度

以下是界面:

@interface IDImage : NSObject <NSCoding>    
@property (nonatomic) const char *templateData;
@property (nonatomic) NSUInteger templateSize;
- (void)encodeWithCoder:(NSCoder *)coder;
- (id)initWithCoder:(NSCoder *)coder;
- (void)encodeWithCoder:(NSCoder *)encoder {
    [encoder encodeBytes:(const unsigned char*)self.templateData length:self.templateSize forKey:@"templateData"];
    [encoder encodeInteger:self.templateSize forKey:@"templateSize"];
}

- (id)initWithCoder:(NSCoder *)decoder {
    NSUInteger length = 0;
    self = [super init];
    if (self) {
        self.templateData = (const char *)[decoder decodeBytesForKey:@"templateData" returnedLength:&length];
        self.templateSize = [decoder decodeIntegerForKey:@"templateSize"];
    }
    return self;
}
我想我正在写出编码的字节(假设NSCoder一直写到最后的第一个字符,也就是templateSize)。但当我尝试解码时,我检索到一个指向templateData的指针。我如何获得该值?如果我有这个值,它只包含第一个字符吗?这意味着我有一个字符和一个长度,但不是我想要解码的实际数据(以字节为单位)


当我的对象仅包含第一个字符和长度时,有人能帮助我理解如何以字节为单位编码和解码数据吗?

如果您查看相关方法的文档
decodeBytesWithReturnedLength:
,它会说:

如果需要超出当前@autoreleasepool范围的字节 块,则必须复制它们

我假设
decodeBytesForKey:returnedLength:
遵循相同的规则。因此,您的代码应该如下所示:

- (id)initWithCoder:(NSCoder *)decoder 
{
  NSUInteger length = 0;
  self = [super init];
  if (self) 
  {
    void* temp = decoder decodeBytesForKey:@"templateData" returnedLength:&length];
    void *bytes = malloc(length);
    memcpy(bytes, temp, length);
    self.templateData = bytes;
    self.templateSize = [decoder decodeIntegerForKey:@"templateSize"];
  }
  return self;
}
然后将如下代码添加到您的dealloc:

- (void) dealloc;
{
  if (_templateData)
    free(_templateData);
)

Tip-o-the-hat to@nil,他在文档中找到了关于返回缓冲区的临时寿命的(相当隐藏的)信息。

此外,我通过了initWithCoder和encodeWithCoder,值是相同的(如预期的),但在调用代码中,值是不同的:templateToRead=[NSKeyEdunachiver unarchiveObjectWithFile:[documentsDirectory stringByAppendingPathComponent:item];是的,在你们两人之间,我终于让它工作了。请注意,如果您的对象没有dealloc方法,您必须添加一个,并确保它释放内存缓冲区。否则将导致内存泄漏。