• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Best Way to Handle Properties?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Best Way to Handle Properties?


  • Subject: Best Way to Handle Properties?
  • From: Dave <email@hidden>
  • Date: Tue, 19 Aug 2008 18:02:55 +0100

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 (email@hidden)

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:
This email sent to email@hidden


  • Follow-Ups:
    • Re: Best Way to Handle Properties?
      • From: Ken Thomases <email@hidden>
    • Re: Best Way to Handle Properties?
      • From: Chris Hanson <email@hidden>
    • Re: Best Way to Handle Properties?
      • From: Jason Coco <email@hidden>
  • Prev by Date: NSATSTypesetter Line computation problem
  • Next by Date: Re: Deploying project as backward compatible
  • Previous by thread: NSATSTypesetter Line computation problem
  • Next by thread: Re: Best Way to Handle Properties?
  • Index(es):
    • Date
    • Thread