问题
I'm having some really REALLY weird issues with NSString. When I read from an input stream and convert the data to a string I'm not able to set anything equal to that string. Here's the code:
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
}
}
I need to copy the string over to another string but I can't. Does anyone know why this is happening?
回答1:
Your byte buffer is one byte big but you're reading 4096 bytes into it. This is likely to trigger a cascading sequence of events that culminates into the crash.
来源:https://stackoverflow.com/questions/41293656/objc-exc-bad-access-when-setting-a-nsstring-equal-to-another