Hi,

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;
}

(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;

The implementation second declares these as "dynamic" (since I may need to "massage" the data after/before getting/setting the object member).

- (NSString*) FirstName ;
- (void) setmFirstName :theNewValue;

- (NSString*) LastName ;
- (void) setLastName :theNewValue;

- (UInt32) DateOfBirth ;
- (void) DateOfBirth :theNewValue;

- (UInt32) Height ;
- (void) Height :theNewValue;


Here are my questions:

1. The strings are read in from a file into a malloc() buffer and are in a non-standard format.

I have a routine that reads strings from the file, converts them to Unicode in the provided buffer. This function is already written (in C) and I'd rather not touch it unless I have to. So I end up with a straight char* buffer containing Unicode characters, I now what to store them in an NSString inside the object, and that's where I'm not sure of the best way of doing it.

The outer loop of the code looks like this (in pseudo code):

PersonDetails*  myPresonDetails;

myPresonDetails = [[PersonDetails alloc] init];

while (myCount > 0)
        {
//Reads a string from the file and places the size in "myStringSize" and the unicode data in "myBufferPtr"
        ReadPersonString("First",&myStringSize,myBufferPtr);

//
//  This is the first bit I am not sure of.
//      
[myPersonDetails setFirstName:[[NSString alloc] initWithCharacters: myBufferPtr length: myStringSize]];

//
//  Do Stuff with myPersonDetails
//

//
//  This is the second bit I am not sure of.
//  Reset (free) the NSString Buffers
//
        [myPersonDetails Reset];

       myCount--;
        }

My specific questions are:

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?

in the: [myPersonDetails Reset]; method I was going to release all the strings and zero out the integers. But I was wondering if it would be better to allocate and release the object each time in the while() loop. If I did this, would I need to define a release method in myPersonDetails or would the standard method release the strings for me?

Thanks in advance
All the Best
Dave




















_______________________________________________

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]

Reply via email to