Objective-C警告,使用未声明的标识符'new'

Hur*_*rkS 1 byte zlib nsdata ios

我收到了这个错误

使用未声明的标识符'new'

在这行代码上

Byte* decompressedBytes = new Byte[COMPRESSION_BLOCK];
Run Code Online (Sandbox Code Playgroud)

这是我的方法,这行代码出现在.

//  Returns the decompressed version if the zlib compressed input data or nil if there was an error
+ (NSData*) dataByDecompressingData:(NSData*)data{
    Byte* bytes = (Byte*)[data bytes];
    NSInteger len = [data length];
    NSMutableData *decompressedData = [[NSMutableData alloc] initWithCapacity:COMPRESSION_BLOCK];
    Byte* decompressedBytes = new Byte[COMPRESSION_BLOCK];

    z_stream stream;
    int err;
    stream.zalloc = (alloc_func)0;
    stream.zfree = (free_func)0;
    stream.opaque = (voidpf)0;

    stream.next_in = bytes;
    err = inflateInit(&stream);
    CHECK_ERR(err, @"inflateInit");

    while (true) {
        stream.avail_in = len - stream.total_in;
        stream.next_out = decompressedBytes;
        stream.avail_out = COMPRESSION_BLOCK;
        err = inflate(&stream, Z_NO_FLUSH);
        [decompressedData appendBytes:decompressedBytes length:(stream.total_out-[decompressedData length])];
        if(err == Z_STREAM_END)
            break;
        CHECK_ERR(err, @"inflate");
    }

    err = inflateEnd(&stream);
    CHECK_ERR(err, @"inflateEnd");

    delete[] decompressedBytes;
    return decompressedData;
}
Run Code Online (Sandbox Code Playgroud)

我不确定为什么会出现这样的情况.这段代码来自于ObjectiveZlib并且已经多次读过它并且我没有尝试在我自己的代码中使用它来解压缩zlib NSData对象,但是这让我阻止了我的进展.

任何帮助将不胜感激.

Lil*_*ard 5

这段代码是Objective-C++.您正在尝试将其编译为Objective-C.将文件重命名为end in .mm而不是.m它应该可以工作.

具体来说,newdelete运算符是C++运算符.它们不存在于C. Objective-C是C的超集,Objective-C++是C++的超集.但是,由于这些似乎是此代码中唯一的C++ - 主义,如果您更愿意使用Objective-C,则可以通过替换两行来修复它:

  1. 替换new Byte[COMPRESSION_BLOCK](Byte*)malloc(sizeof(Byte) * COMPRESSION_BLOCK)
  2. 替换delete[] decompressedBytesfree(decompressedBytes)