Re: Returning structs of NSStrings
Re: Returning structs of NSStrings
- Subject: Re: Returning structs of NSStrings
- From: Angela Brett <email@hidden>
- Date: Fri, 26 Mar 2004 00:48:19 +1300
At 11:26 AM +0100 25/3/2004, Michael Becker wrote:
I've noticed that dealing with large structs of NSStrings can get
really annoying ("copying" these structs can only be done via
copying each NSString in them [right?]). So should I make all of
these into classes, providing accessor methods? Or am I missing out
on something again? Should I maybe even mix Objective-C++ into it
all?
Sorry for asking question that nobody should answer but myself, but
I am more or less asking for stylistic guidance :-)
If you don't think it needs to have its own class just yet, a more
Cocoa-flavoured approach would be to store the information in an
NSDictionary or NSMutableDictionary, then you don't have to worry too
much about memory management for the individual strings as long as
the dictionary itself is properly handled. I've done that several
times and later found that things got more complicated and I had to
write new classes for the information anyway, so it might be better
just to do that from the start. If you really don't want to write
accessor methods, you could use key-value coding instead... just make
a class with the same instance variables as you had in the struct,
and no other code, and then access the variables using [user
valueForKey:@"name" ] and [user takeValue:@"Steve" forKey:@"name"]
(or setValue:@"Steve" forKey:@"name"] if you're targetting 10.3) for
example. You'd probably want to define constants for @"name" and the
other field names though to avoid the danger of mistyping. Then later
if you need more complicated behaviour you can add accessors (with
the appropriate names for KVC) which do more complicated things, and
you won't have to change any code.
As a side note, I notice your little bit of example code has a struct
with name, phone and street in it. I don't know if those field names
were just examples or if they're really what your struct contains,
but perhaps if you're dealing with personal contact details like that
you should consider using the ABPerson class from the Address Book
framework. I have no idea what your application is for but if it is
handling phone numbers and addresses maybe it should be using the
Address Book database and get all the benefits which come with that.
(I watched the WWDC 2003 session on Address Book framework last
night. Those sessions are dangerous, I've already committed myself to
[re]writing a whole heap of code after being inspired by the User
Experience session.)
--
Angela Brett
email@hidden
http://apronyms.com/angela
_______________________________________________
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.