开发者

execute addObject, but NSMutableArray has no object

开发者 https://www.devze.com 2023-04-03 12:19 出处:网络
I try to use to codes below to add object to NSMutableArray NSMutableArray* multipartData; - (void)processDataChunk:(NSData *)postDataChunk

I try to use to codes below to add object to NSMutableArray

NSMutableArray* multipartData;
- (void)processDataChunk:(NSData *)postDataChunk
{
    if (!postHeaderOK)
    {
        UInt16 separatorBytes = 0x0A0D;
        NSData* separatorData = [NSData dataWithBytes:&separatorBytes length:2];

        for (int i = 0; i < [postDataChunk length] - l; i++)
        {
            NSRange searchRange = {i, l};

            if ([[postDataChunk subdataWithRange:searchRange] isEqualToData:separatorData])
            {
                NSRange newDataRange = {dataStartIndex, i - dataStartIndex};
                dataStartIndex = i + l;
                i += l - 1;
                NSData *newData = [postDataChunk subdataWithRange:newDataRange];

                if ([newData length])
                {
                    [multipartData addObject:newData]; //A:set bre开发者_StackOverflow中文版ak point here
                }
            }
        }
    }
}

I set breakpoint at A:, and found that newData is not nil.

What is wrong with my codes?

Welcome any comment


You declare multipartData but do not allocate or initialize it. Somewhere before adding objects you must have

NSMutableArray *multipartData = [[NSMutableArray alloc] init];

or possibly

NSMutableArray *multipartData = [[[NSMutableArray alloc] init] autorelease];

as your needs dictate. But you must allocate and initialize multipartData before adding to or accessing anything in it.

0

精彩评论

暂无评论...
验证码 换一张
取 消