Objc EXC_BAD_ACCESS 当设置一个 NSString 等于另一个
Objc EXC_BAD_ACCESS when setting a NSString equal to another
我在使用 NSString 时遇到了一些非常奇怪的问题。当我从输入流中读取并将数据转换为字符串时,我无法设置任何等于该字符串的内容。这是代码:
NSString *name = r.URL.lastPathComponent;
NSString *data;
NSInputStream *stream = r.HTTPBodyStream;
uint8_t byteBuffer[1];
[stream open];
if (stream)
{
// Get the request body from the stream. Used for setting the file name
if (stream.hasBytesAvailable)
{
NSInteger bytesRead = [stream read:byteBuffer maxLength:4096];
NSString *temp = [[NSString alloc] initWithBytes:byteBuffer length:bytesRead encoding:NSUTF8StringEncoding];
data = temp; // EXC_BAD_ACCESS thrown here
}
}
我需要将字符串复制到另一个字符串,但我做不到。有谁知道为什么会这样?
您的字节缓冲区是一个字节大,但您正在向其中读取 4096 个字节。这可能会触发一系列事件,最终导致崩溃。
我在使用 NSString 时遇到了一些非常奇怪的问题。当我从输入流中读取并将数据转换为字符串时,我无法设置任何等于该字符串的内容。这是代码:
NSString *name = r.URL.lastPathComponent;
NSString *data;
NSInputStream *stream = r.HTTPBodyStream;
uint8_t byteBuffer[1];
[stream open];
if (stream)
{
// Get the request body from the stream. Used for setting the file name
if (stream.hasBytesAvailable)
{
NSInteger bytesRead = [stream read:byteBuffer maxLength:4096];
NSString *temp = [[NSString alloc] initWithBytes:byteBuffer length:bytesRead encoding:NSUTF8StringEncoding];
data = temp; // EXC_BAD_ACCESS thrown here
}
}
我需要将字符串复制到另一个字符串,但我做不到。有谁知道为什么会这样?
您的字节缓冲区是一个字节大,但您正在向其中读取 4096 个字节。这可能会触发一系列事件,最终导致崩溃。