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

标签 ios byte nsdata zlib

我收到此错误

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

在这行代码上

Byte* decompressedBytes = new Byte[COMPRESSION_BLOCK];

这是我的方法,此行代码将出现在其中。
//  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;
}

我不确定为什么会这样出现。这段代码来自ObjectiveZlib,已经阅读了好几次,没有尝试在我自己的代码中使用它来解压缩zlib NSData对象,但是却阻止了我的前进。

任何帮助将不胜感激。

最佳答案

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

具体来说,newdelete运算符是C++运算符。它们在C中不存在。Objective-C是C的超集,而Objective-C++是C++的超集。但是,由于这些似乎是该代码中唯一的C++形式,因此,如果您愿意使用Objective-C,则可以通过替换两行来解决它:

  • new Byte[COMPRESSION_BLOCK]替换(Byte*)malloc(sizeof(Byte) * COMPRESSION_BLOCK)
  • delete[] decompressedBytes替换free(decompressedBytes)
  • 关于ios - Objective-C警告,使用未声明的标识符“new”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13852215/

    相关文章:

    iOS:防止库崩溃整个应用程序

    ios - 在 iOS 应用程序中跨多个 View Controller 共享某些数据的最佳方式是什么

    ios - 当模态呈现另一个 View Controller 时,如何完全关闭 View Controller 的所有后台工作

    swift - 如何快速将浮点值转换为字节数组?

    java - 将 NSData(plist 格式)转换为 Java String

    ios - 如何将 RootViewController 更改为另一个 XIB?

    java - 如何在一个字节中展开位?

    c++ - 如何获取以字符串和其他用户定义类型作为数字的给定结构的大小(以字节为单位)?

    objective-c - NSData 到 NSString,反之亦然

    ios - 将 AVAudioPCMBuffer 转换为 NSData