PDA

View Full Version : Do I have to make two classes?




mikezang
Aug 18, 2010, 04:26 AM
I want to read some data and they have such structures:

@interface StockSplit : NSObject {
NSString *code;
NSString *name;
NSString *market;
int unit;
bool N225;
bool OBS;
NSMutableArray *split
}

The split is a array as below:

struct {
NSDate *date;
float rate;
} SPLIT;


Do I have to make split as a single class, can I put it into class StockSplit or any else simple method?



RonC
Aug 18, 2010, 08:57 AM
You don't HAVE to do anything - you can make the entire array text strings and then interpret the even number elements as one kind of thing and the odd elements as another thing if you like.

However, the right question to be asking is one of yourself: How is the array going to be used?


Will you only be reading them? seems unlikely - how do they get populated?
Will something be editing them too? goes nicely with the above question
Do you expect adding new SPLITs into the array, deleting SPLITs from the array, and modifying the values of existing array elements?


As for the question you asked, I'd make them a separate class, and the array an array (or other collection) of those objects, so it can have its own semantics. Consider the case if you want to add or change the content of a split - you will then have to go around your code and update it.

My personal approach is to hide all the data members behind accessors - if you want to read XXX, use a getXXX: method; if you want to set XXX, use a setXXX: method. For the array, you can have addSplit: and removeSplit: along with getSplit: and setSplit: and getAllSplits: methods.

Yes, it gets complicated, which is why I started with the question instead of an answer.

mikezang
Aug 18, 2010, 09:20 AM
Thanks for your question and answer.
In fact, there is a text file in format as below:
code,name,market,unit,[n225,] [obs,] split
here split in format as below:
S:yyyymmdd:rate [,S:yyyymmdd:rate]
some data like this, total about 5000 lines.

0824,ZZR,T2,1
1203,Bardsoft,T1,100,N225,OBS,S:19940826:1.5
9984,Softbank,T1,100,N225,,S:19950328:1.4,S:19950926:1.4,S:19960326:1.4,S:19960925:1.4,S:19970326:1. 3,S:20000425:3,S:20051228:3

I found when I put them to class, it is very slow, so I am not sure if I have to use class, because use String Array is faster than class, do you think so?