<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Vincent Gable's Blog &#187; NSRange</title>
	<atom:link href="http://vgable.com/blog/tag/nsrange/feed/" rel="self" type="application/rss+xml" />
	<link>http://vgable.com/blog</link>
	<description>my weblog.</description>
	<lastBuildDate>Tue, 29 Nov 2011 22:20:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Beware rangeOf NSString Operations</title>
		<link>http://vgable.com/blog/2009/04/19/beware-rangeof-nsstring-operations/</link>
		<comments>http://vgable.com/blog/2009/04/19/beware-rangeof-nsstring-operations/#comments</comments>
		<pubDate>Sun, 19 Apr 2009 15:42:05 +0000</pubDate>
		<dc:creator>Vincent Gable</dc:creator>
				<category><![CDATA[Bug Bite]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Sample Code]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[NSRange]]></category>
		<category><![CDATA[NSString]]></category>
		<category><![CDATA[Objective-C 2.0]]></category>
		<category><![CDATA[x86]]></category>

		<guid isPermaLink="false">http://vgable.com/blog/2009/04/19/beware-rangeof-nsstring-operations/</guid>
		<description><![CDATA[I have repeatedly had trouble with the rageOf&#8230; NSString methods, because they return a struct. Going forward I will do more to avoid them, here are some ways I plan to do it. Sending a message that returns a struct to nil can &#8220;return&#8221; undefined values. With small structs like NSRange, you are more likely [...]]]></description>
			<content:encoded><![CDATA[<p>I have repeatedly had trouble with the <a href="http://developer.apple.com/DOCUMENTATION/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/rangeOfCharacterFromSet:"><code>rageOf</code>&#8230; <code>NSString</code> methods</a>, because they return a <code>struct</code>. Going forward I will do more to avoid them, here are some ways I plan to do it.</p>
<p><a href="http://vgable.com/blog/2008/05/31/messages-to-nowhere/">Sending a message that returns a <code>struct</code> to <code>nil</code> can &#8220;return&#8221; <em>undefined</em> values</a>. With small <code>struct</code>s like <code>NSRange</code>, you are more likely to get <code>{0}</code> on Intel, compared to PowerPC and iPhone/ARM. Unfortunately, this makes <code>nil</code>-messaging bugs hard to detect.  In my experience <strong>you will miss them when running on the simulator, even if they are 100% reproducible on an actual iPhone</strong>.</p>
<p>This category method has helped me avoid using <code>-rangeOfString:</code> dangerously,</p>
<pre>
@implementation NSString  (HasSubstring)
- (BOOL) hasSubstring:(NSString*)substring;
{
	if(<a href="http://vgable.com/blog/2008/12/16/isempty/">IsEmpty</a>(substring))
		return NO;
	NSRange substringRange = [self rangeOfString:substring];
	return substringRange.location != NSNotFound &#038;&#038; substringRange.length &gt; 0;
}
@end
</pre>
<p>I choose to define <code>[aString hasSubstring:@""]</code> as <code>NO</code>. You might prefer to throw an exception, or differentiate between <code>@""</code> and <code>nil</code>. But I don&#8217;t think a <code>nil</code> string is enough error to throw an exception. And even though technically any string contains the empty string, I generally treat <code>@""</code> as semantically equivalent to <code>nil</code>.</p>
<p>As <a href="http://vgable.com/blog/2008/05/31/messages-to-nowhere/">I&#8217;ve said before</a>,</p>
<blockquote><p>A few simple guidelines can help you avoid my misfortune:</p>
<ul>
<li> Be especially careful using of any objective-C method that returns a <code>double, struct,</code> or <code>long long</code>
<li> Don&#8217;t write methods that return a <code>double</code>, <code>struct</code>, or<code>long long</code>.  Return an object instead of a <code>struct</code>; an <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSNumber_Class/Reference/Reference.html"><code>NSNumber*</code></a> or <code>float</code> instead of a <code>double</code> or <code>long long</code>.  If you must return a dangerous data type, then see if you can avoid it. There really isn&#8217;t a good reason to return a <code>struct</code>, except for efficiency. And when micro-optimizations like that matter, it makes more sense to write that procedure in straight-C, which avoids the overhead of Objective-C message-passing, and solves the undefined-return-value problem.
<li> But if you absolutely must return a dangerous data type, then return it in a parameter. That way you can give it a default value of your choice, and won&#8217;t have undefined values if an object is unexpectedly <code>nil</code>.<br />
Bad:<br />
<code>- (struct CStructure) evaluateThings;</code><br />
Good:<br />
<code>- (void) resultOfEvaluatingThings:(struct CStructure*)result;</code>.
</ul>
</blockquote>
<p>It&#8217;s not a bad idea to wrap up all the <code>rangeOf</code> methods in functions or categories that play safer with <code>nil</code>.</p>
<p><small>Thanks to <a href="http://www.sealiesoftware.com/">Greg Parker</a> for corrections!</small></p>
]]></content:encoded>
			<wfw:commentRss>http://vgable.com/blog/2009/04/19/beware-rangeof-nsstring-operations/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Messages to Nowhere</title>
		<link>http://vgable.com/blog/2008/05/31/messages-to-nowhere/</link>
		<comments>http://vgable.com/blog/2008/05/31/messages-to-nowhere/#comments</comments>
		<pubDate>Sat, 31 May 2008 22:14:47 +0000</pubDate>
		<dc:creator>Vincent Gable</dc:creator>
				<category><![CDATA[Bug Bite]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[MacOSX]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[nil]]></category>
		<category><![CDATA[NSRange]]></category>
		<category><![CDATA[NSString]]></category>
		<category><![CDATA[Objective-C 1.0]]></category>

		<guid isPermaLink="false">http://vgable.com/blog/2008/05/31/messages-to-nowhere/</guid>
		<description><![CDATA[If you send a message to (call a method of) an object that is nil (NULL) in Objective-C, nothing happens, and the result of the message is nil (aka 0, aka 0.0, aka NO aka false). At least most of the time. There is an important exception if sizeof(return_type) &#62; sizeof(void*); then the return-value is [...]]]></description>
			<content:encoded><![CDATA[<p>If you send a message to (call a method of) an object that is <code>nil</code> (<code>NULL</code>) in Objective-C, nothing happens, and the result of the message is <code>nil</code> (aka <code>0</code>, aka <code>0.0</code>, aka <code>NO</code> aka <code>false</code>).  At least most of the time. There is an important exception if <code>sizeof(return_type) &gt; sizeof(void*)</code>; then the return-value is <b>undefined</b> under PowerPC/iPhone, and thus all Macs for the next several years.  So watch out if you are using a return value that is a <code> struct, double, long double,</code> or <code>long long</code>.</p>
<p><a href="http://developer.apple.com/documentation/MacOSX/Conceptual/universal_binary/universal_binary_tips/chapter_5_section_22.html">The fully story:</a></p>
<blockquote><p>
In Objective-C, it is valid to send a message to a <code>nil</code> object. The Objective-C runtime assumes that the return value of a message sent to a <code>nil</code> object is <code>nil</code>, as long as the message returns an object or any integer scalar of size less than or equal to <code>sizeof(void*).</code><br />
On Intel-based Macintosh computers, messages to a <code>nil</code> object always return <code>0.0</code> for methods whose return type is <code> float, double, long double, </code> or <code>long long.</code> Methods whose return value is a <code>struct</code>, as defined by the <a href="http://developer.apple.com/documentation/DeveloperTools/Conceptual/LowLevelABI/index.html#//apple_ref/doc/uid/TP40002521"> Mac OS X ABI Function Call Guide</a> to be returned in registers, will return <code>0.0</code> for every field in the data structure. Other <code>struct</code> data types will not be filled with zeros. This is also true under Rosetta. On PowerPC Macintosh computers, the behavior is undefined.</p></blockquote>
<p>I was recently bitten by this exceptional behavior.  I was using an <code>NSRange</code> <code>struct</code> describing a substring;  but the string was <code>nil</code>, so the substring was garbage.  But only on a PPC machine!  Even running under Rosetta wouldn&#8217;t have reproduced the bug on my MacBook Pro.Undefined values can be a hard bug to detect, because they may be reasonable values when tests are run.</p>
<p>Code running in the iPhone simulator will return all-zero stucts when messaging <code>nil</code>, but the same code running on an actual device will return undefined structs. Be aware that <strong>testing in the simulator isn&#8217;t enough to catch these bugs</strong>.</p>
<p>A few simple guidelines can help you avoid my misfortune:</p>
<ul>
<li> Be especially careful using of any objective-C method that returns a <code>double, struct,</code> or <code>long long</code>
<li> Don&#8217;t write methods that return a <code>double</code>, <code>struct</code>, or<code>long long</code>.  Return an object instead of a <code>struct</code>; an <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSNumber_Class/Reference/Reference.html"><code>NSNumber*</code></a> or <code>float</code> instead of a <code>double</code> or <code>long long</code>.  If you must return a dangerous data type, then see if you can avoid it. There really isn&#8217;t a good reason to return a <code>struct</code>, except for efficiency. And when micro-optimizations like that matter, it makes more sense to write that procedure in straight-C, which avoids the overhead of Objective-C message-passing, and solves the undefined-return-value problem.
<li> But if you absolutely must return a dangerous data type, then return it in a parameter.<br />
Bad:<br />
<code>- (double) crazyMath;</code><br />
Good:<br />
<code>- (void) crazyMathResult:(double*)result;</code>.
</ul>
<p>I love Objective-C&#8217;s &#8220;nil messaging&#8221; behavior, even though it is rough around the edges.  It&#8217;s usefulness is beyond the scope of this article, but it can simplify your code if you don&#8217;t return a data-type that is larger then <code>sizeof(void*)</code>.  With time, when the intel-style return behavior can be universally relied on, things will be even better.</p>
]]></content:encoded>
			<wfw:commentRss>http://vgable.com/blog/2008/05/31/messages-to-nowhere/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

