Forum Navigation
You need to log in to create posts and topics.

[OT]Different behavior for NSDictionary dictionaryWithObjectsAndKeys vs. literal container syntax

Posted by: fblistserve <fblistserve@...>

Was updating code and thought this behavior worth sharing ( apologies if it's been covered ). This is Objective-C but potentially applies to FB code.


This code creates a dictionary with one key because the only non-nil value is customer name ( with a key of kClientCustNameKey ) even though all values are possibly non-nil depending on data entered.  All is good at runtime. 

        NSDictionary *returnedDict = [NSDictionary dictionaryWithObjectsAndKeys:
            [dict objectForKey:kClientCustNameKey],    ColClientNameClientCustSheet,
            [dict objectForKey:kClientCustAddressKey], ColClientAddrClientCustSheet,
            [dict objectForKey:kClientCustNoteKey],    ColClientNoteClientCustSheet,
            [dict objectForKey:kClientCustEmailKey],   ColClientEmailClientCustSheet,
            [dict objectForKey:kClientCustWebPageKey], ColClientWebClientCustSheet,
            [dict objectForKey:kClientCustPhoneKey],   ColClientPhoneClientCustSheet,
            nil];

However, this code ( via the collection literal syntax ) generates a call to  [NSDictionary dictionaryWithObjects:forKeys:count:]
which crashes ( in a later internal processing call ) because it finds a nil value.

       NSDictionary *returnedDict = @{ ColClientNameClientCustSheet:  dict[kClientCustNameKey],
                                        ColClientAddrClientCustSheet:  dict[kClientCustAddressKey],
                                        ColClientNoteClientCustSheet:  dict[kClientCustNoteKey],
                                        ColClientEmailClientCustSheet: dict[kClientCustEmailKey],
                                        ColClientWebClientCustSheet:   dict[kClientCustWebPageKey],
                                        ColClientPhoneClientCustSheet: dict[kClientCustPhoneKey]
        };


--
To unsubscribe, send ANY message to: futurebasic-unsubscribe@welovegod.org To access the list archives, go to: http://welovegod.org/groups/futurebasic/