I’m newer to Objective-C though I have worked in Java, C, and C++ and I’m still learning Objective-C.
I have a socket, a receive data function, and a text view. As data comes in, I want to append it to the text view. Now my textview has some pre-populated text at the start. If in every call to receieve data that data comes in, I just get the current text of the text view, append it to itself in a nsstring with:
NSString *oldtext = [mTextViewAlias text];
NSString *toSend = [oldtext stringByAppendingString: oldtext];
and then set the text view to toSend, it works fine, and I see the data grow in my text view.
The problem is, I want to append:
UInt8 buffer[len]; // which has data from the socket. len is set to amount of data on each call of receive data as follows
int len = CFDataGetLength(df);
What I’ve been trying to do is convert a buffer to a nsstring and append. for example:
NSString *newdata = [NSString stringWithUTF8String: buffer];
it’s occurred to me that buffer may not terminate with a '\0' character so I’ve even created a new buffer called char newbuffer[len+1]; and copied buffer into it and added a \0 as the last character.
I can append the first time around on the first pass of new data, but the second append, appending to something that had chars from buffer appended to the text once before always crashes.
I did a little trick where if len > 10 assign buffer[10]='\0'. And it actually let me grab data twice before crashing the third time.
It seems I have one of two problems and I’m not sure how to fix it. One is I can only grab as much data as is in the buffer and somehow these nsstrings are depending on it and when I change the buffer when receiving data, it is called again it causes the crash. Or maybe it’s just still an issue with the '\0' not being there still though I don’t see how.
Try something like this:
Full reference on the encoding types here.
No guarantees this works as I wrote this code from a Windows machine and a couple tabs open at Apple’s developer site. But should help you in the right direction.