Re: NSDictionary dictionaryWithContentsOfFile in Carbon app
Re: NSDictionary dictionaryWithContentsOfFile in Carbon app
- Subject: Re: NSDictionary dictionaryWithContentsOfFile in Carbon app
- From: Uli Kusterer <email@hidden>
- Date: Sun, 8 Oct 2006 23:12:04 +0200
Am 08.10.2006 um 20:51 schrieb Roni Music:
Would this modified example be correct?
In principle, yes. But you need to get your variable names right:
You sometimes wrote anArray and aDictionary where it should be
m_anArray and m_aDictionary :-p
foo:: foo()
{
m_aDictionary = 0;
m_anArray = 0;
}
Just a small style thing: It's a pointer, not an int, so you
*should* really be assigning this NULL instead of zero to make that
clear. Also, in ObjC the convention is to use the constant "nil" to
mean "an uninitialized object" (it's basically the same as NULL, but
I use the three to help me distinguish in code: 0 is an int, 0.0 a
float, NULL a classic pointer or C++ object and nil an ObjC object.
// this function will be called several times
// creates the dictonary and array objects
void foo::bar()
{
CAutoRelease autoReleaseMe;
.....
[m_aDictionary release]; // OK to release even if it's NULL? (which
it is the first time being called)
aDictionary = [NSDictionary dictionaryWithContentsOfFile:[aString
stringByExpandingTildeInPath]];
[m_aDictionary retain];
[m_anArray release];
anArray = [aDictionary objectForKey: @"aKey"];
[m_anArray retain];
......
//now use the array
}
Yes, if you look at Apple's Objective C book <http://
developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/
ObjC.pdf> on page 42, you'll see that you can safely send messages to
NIL as long as the method's return type is one of a select few. Since
the return type of retain is void, that's OK, too. (Though I'm kinda
surprised the docs don't mention "void" as being OK, I should
probably file a bug about that).
Cheers,
-- M. Uli Kusterer
http://www.zathras.de
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden