<?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>Codec &#187; NSMutableArray</title>
	<atom:link href="http://www.trembl.org/codec/tag/nsmutablearray/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.trembl.org/codec</link>
	<description>A Personal Polylogic Code/Decode &#039;Zettelkasten&#039;</description>
	<lastBuildDate>Fri, 10 Sep 2010 09:12:16 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>NSMutableArray: setObject vs. setValue</title>
		<link>http://www.trembl.org/codec/214/</link>
		<comments>http://www.trembl.org/codec/214/#comments</comments>
		<pubDate>Tue, 10 Nov 2009 03:53:18 +0000</pubDate>
		<dc:creator>Georg Tremmel</dc:creator>
				<category><![CDATA[Raw]]></category>
		<category><![CDATA[nil]]></category>
		<category><![CDATA[NSMutableArray]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[setObject]]></category>
		<category><![CDATA[setValue]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.trembl.org/codec/?p=214</guid>
		<description><![CDATA[Following situation Using TouchXML to parse XML data, works without problem on example XML files, but crahes on mine. Problem Empty values in my data set. (sometimes &#60;place&#62;somePlace&#60;/place&#62;, sometimes &#60;place&#62;&#60;/place&#62;) /* CXMLDocument setup &#38; parsing omitted */ NSString *e = &#91;&#91;resultElement childAtIndex:counter&#93; stringValue&#93;; NSString *k = &#91;&#91;resultElement childAtIndex:counter&#93; name&#93;; &#160; &#160; &#91;blogItem setObject:e forKey:k&#93;; // [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Following situation</strong> Using TouchXML to parse XML data, works without problem on example XML files, but crahes on mine.<br />
<strong>Problem</strong> Empty values in my data set. (sometimes &lt;place&gt;somePlace&lt;/place&gt;, sometimes &lt;place&gt;&lt;/place&gt;)</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">/* CXMLDocument setup &amp; parsing omitted */</span>
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>e <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>resultElement childAtIndex<span style="color: #002200;">:</span>counter<span style="color: #002200;">&#93;</span> stringValue<span style="color: #002200;">&#93;</span>;
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>k <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>resultElement childAtIndex<span style="color: #002200;">:</span>counter<span style="color: #002200;">&#93;</span> name<span style="color: #002200;">&#93;</span>;
&nbsp;
&nbsp;
<span style="color: #002200;">&#91;</span>blogItem setObject<span style="color: #002200;">:</span>e forKey<span style="color: #002200;">:</span>k<span style="color: #002200;">&#93;</span>;
<span style="color: #11740a; font-style: italic;">// crashes when e is empty. Displays a (null), is nil</span></pre></div></div>

<p><strong>Solution 1</strong><br />
Check for empty e, replace nil with empty string</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// check if element is empty</span>
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> <span style="color: #a61390;">nil</span> <span style="color: #002200;">==</span> e <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>	<span style="color: #11740a; font-style: italic;">// ...or the less elegant (0 == [e length])</span>
	e <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;&quot;</span>;
<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#91;</span>blogItem setObject<span style="color: #002200;">:</span>e forKey<span style="color: #002200;">:</span>k<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p><strong>Solution 2</strong><br />
Use <em>setValue</em> instead of <em>setObject</em>, as setObject crashes and burns when it encounters nil, whereas setValue specifically deals only with strings and handles nil gracefully.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span>blogItem setValue<span style="color: #002200;">:</span>e forKey<span style="color: #002200;">:</span>k<span style="color: #002200;">&#93;</span>;</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.trembl.org/codec/214/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
