iOS NSMutableData * data = [[NSMutableData alloc] init];崩溃

时间:2012-11-28 07:11:57

标签: objective-c ios

我在函数中使用下面提到的行

NSMutableData *data = [[NSMutableData alloc] init];  

我称这个函数没有时间(例如100次)。所以我的问题是,最初大约60次或更多它正常工作,但之后它给了我“BAD_EXC_ACCESS”

功能如下

+ (NSString *) recvToFile:(NSString *)_fileName {

@try {
    int _sz = [self recvNumber:4];
    uint8_t t[_sz];
    NSMutableData *data = [[NSMutableData alloc] init];
    NSMutableData *fileData = [[NSMutableData alloc] init];
        long _pos = 0;
        NSString *_fullPath = _fileName;
        while (_sz > _pos) {
            long _c = [m_sin read:t maxLength:_sz];
            _pos += _c;
            data = [NSData dataWithBytes:t length:_c];
            if([Misc checkFileExists:_fileName]==nil)
                [[NSFileManager defaultManager] createFileAtPath:_fullPath contents:nil attributes:nil];
            [fileData appendData:data];
        }
    [fileData writeToFile:_fullPath atomically:YES];
    NSDictionary *attr = [[NSFileManager defaultManager] attributesOfItemAtPath:_fullPath error:nil];

    long long length = [[attr valueForKey:@"NSFileSize"] intValue];
    if (length >= _sz)
        return (_fullPath);
}
@catch (NSException * e) {
}
return (nil);
}

每次收到文件时我都会调用此函数。我想从字节中保存文件

2 个答案:

答案 0 :(得分:0)

您不应在此处初始化数据:

NSMutableData *data = [[NSMutableData alloc] init];

只需将变量初始化为nil;

NSMutableData* data = nil;

答案 1 :(得分:0)

尝试将此类数据添加到nsmutabledata

    [data appendBytes:t length:_c];