Re: NSData weirdness...
Re: NSData weirdness...
- Subject: Re: NSData weirdness...
- From: Sherm Pendley <email@hidden>
- Date: Thu, 20 Jun 2002 23:43:52 -0400
On Thursday, June 20, 2002, at 11:22 PM, Warwick Hall wrote:
NSData* data = [contents subdataWithRange : range];
NSLog(@"%s", [data bytes]);
Does anyone have a clue why this is all happening to me?
C strings are assumed to be null-terminated by default, so unless you
use a precision modifier in the format string, NSLog() will keep
printing characters until it hits a null. Quite often, that would
produce gibberish, as it did in your first attempt, or possibly a
segfault. But, because "data" is not a copy, just a pointer into the
same bytes that are held in "contents", it keeps going merrily on its
way, until it gets to the end of "contents".
To avoid that, you can use a precision modifier in your format string.
For example, to restrict NSLog() to printing a maximum of a hundred
characters, you'd use this:
NSLog(@"%.100s", [data bytes]);
You can also specify the minimum number of characters to print, with a
number to the left of the decimal point. So, to print at least fifty
characters, or at most a hundred:
NSLog(@"P.100s", [data bytes]);
If the string is shorter than the minimum number of characters you've
specified, the output is padded with spaces.
Hope this helps!
sherm--
Never put off until tomorrow what you can do today. There might be a law
against it by that time.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.