On Aug 19, 2008, at 10:02 AM, Dave wrote:
I'm new to Cocoa and a little confused about the best way to handle
the following:
I'm converting a C based application to Object-C/Cocoa. One of the
objects is used to hold properties which are loaded from a data
file. I have created an object to represent the C Structure as so:
@interface PersonDetails : NSObject
{
NSString* FirstName;
NSString* LastName;
UInt32 DateOfBirth;
UInt32 Height;
}
In Cocoa development, it's best to follow the Cocoa coding
conventions. That includes things like naming: Some of Cocoa's
dynamic behavior actually leverages the naming conventions, as in Key-
Value Coding.
Thus you should name these instance variables firstName, lastName,
dateOfBirth, and height, respectively.
(This is a cut down version of the real object).
@property(assign,readwrite) NSString* FirstName;
@property(assign,readwrite) NSString* LastName;
@property(assign,readwrite) UInt32 DateOfBirth;
@property(assign,readwrite) UInt32 Height;
These properties should also be named using an initial lower-case
letter to fit Cocoa conventions.
Furthermore, in general "assign" is the wrong thing for most object
properties (unless they're a parent/peer, like an owner or delegate
would be). Even more importantly, for value classes like NSString
that have mutable subclasses, you should always use "copy" instead of
retain.
The implementation second declares these as "dynamic" (since I may
need to "massage" the data after/before getting/setting the object
member).
You don't need to declare the property as @dynamic if you are going to
provide an implementation. In fact, you probably WON'T want to
declare a property @dynamic in the @implementation block - if you do,
you won't get a warning from the compiler if you fail to implement it.
- (NSString*) FirstName ;
- (void) setmFirstName :theNewValue;
- (NSString*) LastName ;
- (void) setLastName :theNewValue;
- (UInt32) DateOfBirth ;
- (void) DateOfBirth :theNewValue;
- (UInt32) Height ;
- (void) Height :theNewValue;
You don't need method declarations in addition to the @property
declarations -- those are actually redundant with the @property
declarations.
Also, the colon is itself part of the method name, you shouldn't
separate it like you are above.
Is the NSString allocation and initWithCharacters code the best way
to do this? If so, what would the setter look like in this case? If
not what is a better way of doing it?
How to write your custom accessors is addressed well in the Cocoa
memory management documentation. Your setters will, in general,
always follow one of only a small number of patterns and those are
covered in the documentation.
-- Chris
_______________________________________________
Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com
This email sent to [EMAIL PROTECTED]