将 nsdata 对象合并为 nsmutabledata 对象

发布于 2025-01-08 16:05:34 字数 1051 浏览 0 评论 0原文

我相当确定我必须使用 NSMutableData 来解决这个问题,因为我将多次访问该对象并在获得数据后添加每个数据部分。

我面临的问题是我想要创建一个大的 NSMutableData 对象,该对象将由几个附加到可变数据对象末尾的小 NSData 对象创建,

我已经尝试了以下操作。

编辑://此方法现在可以正常工作并按预期附加数据。

- (void) constructRequest
{
    NSData * protocolInt = [self addProtocolVersion];
    NSMutableData * myMutableData = [[NSMutableData alloc] init];

    NSData *first_data = [self addProSig]; //nsdata type
    NSData *second_data = [self addAct]; //nsdata type
    [myMutableData appendData:first_data];
    [myMutableData appendData:second_data];
    //etc


    [protocolInt writeToFile:@"/Users/imac/Desktop/_dataDump.dat" atomically:YES];

}

首先,我什至不确定这是否是附加数据的正确方法,只是我看过几个示例相似的。 主要问题是,在这里的两行上

NSMutableData *first_data = [self addProSig]; //nsdata type
        NSMutableData *second_data = [self addAct]; //nsdata type

我都有警告

初始化“NSMutableData * _strong”时不兼容的指针类型 'NSData *' 类型的表达式

任何帮助将不胜感激,也可能是我正在使用的更好的解决方案(如果有的话)。

I'm fairly sure I have to use NSMutableData for this problem as I will be accessing the object several times and adding each section of data once I have it.

The problem I am faced with is that I am wanting to create one big NSMutableData object that will be created by several small NSData objects that are append to the end of the mutable data object

I have tried the following.

EDIT: // This method now works and appends the data as its supposed too.

- (void) constructRequest
{
    NSData * protocolInt = [self addProtocolVersion];
    NSMutableData * myMutableData = [[NSMutableData alloc] init];

    NSData *first_data = [self addProSig]; //nsdata type
    NSData *second_data = [self addAct]; //nsdata type
    [myMutableData appendData:first_data];
    [myMutableData appendData:second_data];
    //etc


    [protocolInt writeToFile:@"/Users/imac/Desktop/_dataDump.dat" atomically:YES];

}

First of all I am not even sure if this is the correct way to append data, It's just that I have seen several examples similar.
The main issue is that on the two lines here

NSMutableData *first_data = [self addProSig]; //nsdata type
        NSMutableData *second_data = [self addAct]; //nsdata type

I have warnings on both lines

incompatible pointer types initializing 'NSMutableData * _strong' wuth
an expression of type 'NSData *'

any help would be appreciated, Also possible better solutions that what I am using if there are any.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半寸时光 2025-01-15 16:05:34

要摆脱这些警告,您可以像这样制作可变副本......

NSMutableData *first_data = [[self addProSig] mutableCopy]; //nsdata type
NSMutableData *second_data = [[self addAct] mutableCopy]; //nsdata type

To get rid of those warnings you can make a mutable copy like this...

NSMutableData *first_data = [[self addProSig] mutableCopy]; //nsdata type
NSMutableData *second_data = [[self addAct] mutableCopy]; //nsdata type
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文